I have applied DataAnnotation based validations to two of my properties like this
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }
[Required(ErrorMessage = "Description is required")]
public string Description { get; set; }
Here is the view page's code
@Html.LabelFor(model => model.Obj.Title)
@Html.EditorFor(model => model.Obj.Title)
@Html.LabelFor(model => model.Obj.Description)
@Html.TextAreaFor(model => model.Obj.Description)
The Problem is that on click of submit button, on client side (js) its only giving me error for for Title and not for the Description. But Its giving me validation error for the Description a开发者_StackOverflow中文版fter the postback.
What possible causes?
Hmm, that's weird. Unable to repro.
Model:
public class MyViewModel
{
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }
[Required(ErrorMessage = "Description is required")]
public string Description { get; set; }
}
public class MainViewModel
{
public MyViewModel Obj { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MainViewModel { Obj = new MyViewModel() });
}
}
View:
@model AppName.Models.MainViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.Obj.Title)
@Html.EditorFor(model => model.Obj.Title)
@Html.LabelFor(model => model.Obj.Description)
@Html.TextAreaFor(model => model.Obj.Description)
<input type="submit" value="OK" />
}
Client and server validation work fine.
精彩评论