Monday, August 13, 2012

@Html.ValidationMessageFor

using System;
using System.Web.Mvc;
using KeepFeelingValidation.Models;
namespace KeepFeelingValidation.Controllers
{
   public class HomeController : Controller
   {
      public ActionResult Index()
      {
         Person person = new Person();
         person.Name = "Adolph Coors I";
         person.AgeAtDeath = 82;
         person.BirthDay = new DateTime(1847, 2, 4);
         person.IsCrazy = true;
         person.Id = Guid.NewGuid();
         return View(person);
      }
      
      public ActionResult About(Person person)
      {
         return View();
      }
   }
}

 
 

I put these two lines in the master page:

  1. <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
  2. <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

 
 

My home view looked like so:

@model KeepFeelingValidation.Models.Person
@{
   ViewBag.Title = "Home Page";
}
@{ Html.EnableClientValidation(); }
@Html.ValidationSummary(true, "Test was unsuccessful.")
@using (Html.BeginForm("About", "Home", FormMethod.Post, new { }))
{
   @Html.LabelFor(m => m.Name)
   @Html.TextBoxFor(m => m.Name)
   @Html.ValidationMessageFor(m => m.Name)
   <hr/>
   @Html.LabelFor(m => m.AgeAtDeath)
   @Html.EditorFor(m => m.AgeAtDeath)
   @Html.ValidationMessageFor(m => m.AgeAtDeath)
   <hr/>
   @Html.LabelFor(m => m.BirthDay)
   @Html.EditorFor(m => m.BirthDay)
   @Html.ValidationMessageFor(m => m.BirthDay)
   <hr/>
   @Html.LabelFor(m => m.IsCrazy)
   @Html.CheckBoxFor(m => m.IsCrazy)
   @Html.ValidationMessageFor(m => m.IsCrazy)
   <hr/>
   @Html.HiddenFor(m => m.Id)
   <input type="submit" value="submit" />
}

No comments:

Post a Comment