MVC Required field validation not working
Asked Answered
F

2

8

Required field validation not firing.

Model:

public class Client
    {
        [Required(ErrorMessage="Name Required")]
        [DisplayFormat(ConvertEmptyStringToNull = false)]
        public string Name { get; set; }
        public string Address { get; set; }
        public string Mobile { get; set; }
        public string Telephone { get; set; }
        public string Fax { get; set; }
        public string Company { get; set; }
    }

Controller:

 [HttpPost]
        public ActionResult Create(Client client)
        {
            try
            {
                // TODO: Add insert logic here
                ClientRepository rep = new ClientRepository();
                rep.AddClient(client);
                rep.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

View:

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Add New Client</h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Name) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Name) %>
                <%: Html.ValidationMessageFor(model => model.Name) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Address) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Address) %>
                <%: Html.ValidationMessageFor(model => model.Address) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Mobile) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Mobile) %>
                <%: Html.ValidationMessageFor(model => model.Mobile) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Telephone) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Telephone) %>
                <%: Html.ValidationMessageFor(model => model.Telephone) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Fax) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Fax) %>
                <%: Html.ValidationMessageFor(model => model.Fax) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Company) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Company) %>
                <%: Html.ValidationMessageFor(model => model.Company) %>
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>
Fogel answered 29/4, 2013 at 2:25 Comment(2)
Why server side validation and not Client sideNim
@Nim Just for the records, you must never trust in user input. Client side validation can be disabled-manipulated-faked, etc, so server side validation is always necessary.Encroach
G
15

Use ModelState.IsValid property

public ActionResult Create(Client client)
{

  if(ModelState.IsValid)
   {
   // TODO: Add code here
      ClientRepository rep = new ClientRepository();
      rep.AddClient(client);
      rep.Save();
      return RedirectToAction("Index");
   }
   return View(client);
} 
Gem answered 29/4, 2013 at 2:33 Comment(1)
I think this is not the answer. This is a other way to validate fields.Boracite
K
0

For required field validation decorate your function with [BindRequired(ErrorMessage = "Name Required")] instead of using [Required(ErrorMessage="Name Required")]

Kessiah answered 9/9, 2020 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.