3

I am using MVC4.

Validation is failing but validation error messages are not getting displayed.

This is my model.

public class Configuration
{
    public int Id { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "Site name is required.")]
    [MinLength(6, ErrorMessage = "Name should be at least 6 characters.")]
    public string SiteName { get; set; }
}

Controller.

[HttpPost]
    public ActionResult Create(Configuration configItem)
    {
        if (ModelState.IsValid)
        {
            // do something.
        }
        return View("Index", configItem);
    }

View is

@model Models.SitesConfig.Configuration
@{
ViewBag.Title = "Sites Configurations";
}
<div>
    @Html.ActionLink("Sites List", "List", "SiteConfig")
</div>
@using (Html.BeginForm("Create", "SiteConfig", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <fieldset>
        <legend>New Satellitesite</legend>
        <div>
            @Html.LabelFor(m => m.SiteName, "Name")
            @Html.TextBoxFor(m => m.SiteName)
            @Html.ValidationMessageFor(m=>m.SiteName)
        </div>
        <br />

        <input type="submit" value="Save" />

    </fieldset>
}

Please also suggest me if there is any better way of doing the validations.

Naresh
  • 2,667
  • 13
  • 44
  • 69
  • And how do you know that the validation is working if there is no error displayed? Are your talking about client or server side validation? – nemesv Sep 06 '12 at 11:04
  • It is server side validation. Model.IsValid is returning false. – Naresh Sep 06 '12 at 11:14
  • Your code looks fine btw. Is the view what you've shown is the `Index.cshtml` and you don't do anything with the ModelState collection in your controller action? – nemesv Sep 06 '12 at 11:15
  • 2
    Have you referenced the `Site.css` in your layout? Or do you have custom styles for the validation message? Maybe the validation messages in the html output and they are just not shown... Can you check the generated html after hitting the submit? – nemesv Sep 06 '12 at 11:21
  • I don't how, but it is now started showing the errors. Cleaned and built the solution again. But I am facing another problem. – Naresh Sep 06 '12 at 11:39

5 Answers5

4

I don't know what was the problem. After clean and build the application, I am able to see the error message.

Thanks, Naresh

Naresh
  • 2,667
  • 13
  • 44
  • 69
1

There should be @Html.ValidationSummary("Please correct the errors") in the View

Hari Gillala
  • 11,736
  • 18
  • 70
  • 117
  • 4
    The `@Html.ValidationMessageFor(m=>m.SiteName)` should be able to display a single error message for the `SiteName` property. Summary should not be needed. – Nope Sep 06 '12 at 11:11
0
@using (Html.BeginForm("Create", "SiteConfig", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  @Html.ValidationSummary()
    <fieldset>
        <legend>New Satellitesite</legend>
        <div>
            @Html.LabelFor(m => m.SiteName, "Name")
            @Html.TextBoxFor(m => m.SiteName)
            @Html.ValidationMessageFor(m=>m.SiteName)
        </div>
        <br />

        <input type="submit" value="Save" />

    </fieldset>
}

Try this one .. @Html.ValidationSummary() helps in displaying the error messages.

Code Geek
  • 143
  • 1
  • 6
0

If you try the above solutions and still didn't work, then maybe you need to format your View file. Go to Edit-Advanced-Format Document. Remember not to be in the debug mode when trying to format your file because you wouldn't see the above listed procedures if you are debugging your project.

Alf Moh
  • 7,159
  • 5
  • 41
  • 50
0

Make sure your View is referencing the jQuery validations in the Scripts tag -

@section Scripts
{
    @Scripts.Render("~/bundles/jqueryval")
}
Ron
  • 1,901
  • 4
  • 19
  • 38