Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Required field validation not working

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>
like image 504
chamara Avatar asked Apr 29 '13 02:04

chamara


People also ask

How to set required field in mvc?

From the model class drop down, select "Employee" as the model. Click on the "Add" button. It will automatically create the view from the model. Add the "[Required]" attribute to the field that you want to make mandatory for insertion.

How can use client side validation in ASP.NET MVC?

Firstly, you just need to create an ASP.NET MVC application. To create a new ASP.NET MVC application, Open Visual Studio choose File, New, then Project. It will open a New Project window, from where you need to choose node Visual C# and then Web and from the right pane you need to choose ASP.NET Web Application.

What is data annotations in MVC?

DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.


1 Answers

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);
} 
like image 121
KV Prajapati Avatar answered Sep 27 '22 19:09

KV Prajapati