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.
Index.cshtmland you don't do anything with the ModelState collection in your controller action?Site.cssin 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?