I am trying to get what, in my opinion, is a simple example of using DataAnnotations on a model to validate client-side validation.
Here is my model ...
public class Person { [Required(ErrorMessage = "First Name Required")] public string FirstName { get; set; } [Required(ErrorMessage = "Last Name Required")] public string LastName { get; set; } }
Here is my controller ...
public class FriendsController : Controller { public ActionResult Create() { Person newFriend = new Person(); return View(newFriend); } [HttpPost] public ActionResult Create(Person friendToCreate) { if (ModelState.IsValid) {
and here is my opinion ...
@model MvcApplication4.Models.Person <!DOCTYPE html> <html> <head> <title>@ViewBag.Title</title> <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/jquery-1.4.4.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"></script> </head> <body> <h2> Create</h2> @{Html.EnableClientValidation();} @using (Html.BeginForm()) { <fieldset> <p> @Html.LabelFor(m => m.FirstName) @Html.TextBoxFor(m => m.FirstName) @Html.ValidationMessageFor(m => m.FirstName) </p> <p> @Html.LabelFor(m => m.LastName) @Html.TextBoxFor(m => m.LastName) @Html.ValidationMessageFor(m => m.LastName) </p> <p> <input type="submit" value="Create" /> </p> </fieldset> } </body> </html>
Server-side validation works very well, and validation error messages are displayed as expected. However, I do not receive client-side validation confirmation. Is there something obvious that I'm missing to do a client-side check?
validation asp.net-mvc asp.net-mvc-3 data-annotations
John Livermore Dec 19 '10 at 23:47 2010-12-19 23:47
source share