2

I have an application in which there is a user and retailer account. I want to implement validation of retailer registration form. I made a model for retailer with Required Connotation but the page still doesn't show any messages for wrong inputs. Any suggestion how I can achieve my goal. Im using Asp.net MVC

[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")]
public class RegisterStore
{
    [Required(AllowEmptyStrings = false, ErrorMessage = "Store Name is Required")]
    [DataType(DataType.Text)]
    [Display(Name = "Store Name")]
    public string Store_Name { get; set; }
  .
  .       
    Similarly other properties ...
  .
  .

}

The code for View is like

 <h2>Create a Store Account</h2>
<p>
    Use the form below to create a new account. 
</p>
<% using (Html.BeginForm())
   {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Retailer Information</legend>

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

        ... Similar DIVs for other properties ....
        .
        .
        .
        .

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

<% } %>

The code for controller is like

  [HttpPost()]
    public ActionResult RetailerRegisteration(RegisterStore storeModel)
    {
        //ViewData["genders"] = Genders;
        Debug.WriteLine("Started RetailerRegisteration");
        if (string.IsNullOrEmpty(storeModel.UserName))
            ModelState.AddModelError(string.Empty, "Please enter Username");     
        if (string.IsNullOrEmpty(storeModel.Store_Name))
            ModelState.AddModelError(string.Empty, "Please enter a store name");
        if (!string.IsNullOrEmpty(storeModel.Email) || !storeModel.Email.Contains("@"))
            ModelState.AddModelError(string.Empty, "Please enter a valid e-mail address!");
        if (string.IsNullOrEmpty(storeModel.Password))
            ModelState.AddModelError(string.Empty, "Please enter a Password");
        if(! storeModel.Password.Equals(storeModel.ConfirmPassword))
            ModelState.AddModelError(string.Empty, "The Passwords must match");

        if (ModelState.IsValid)
        {
           ... Create Store Account ....

        }
Waheed Khan
  • 1,323
  • 6
  • 18
  • 28

3 Answers3

2

Try <%: Html.ValidationSummary(false) %> to show all errors.

Also, please refer to this question.

Community
  • 1
  • 1
O.O
  • 11,077
  • 18
  • 94
  • 182
2

Are you using client validation or server-side validation only?

If you're checking server-side, are you checking ModelState.IsValid on POST? The validation won't actually fire until you check this property.

Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62
0

Are you using MVC2 or MVC3? There are subtle differences between the two. In either case, you must include the appropriate jQuery or Ajax validation JavaScript scripts to get client side validation.