关于收集MVC 3客户端验证与数据的注解 - 不工作 [英] MVC 3 client-side validation on collection with data annotations -- not working
问题描述
我想获得客户端验证在我的模型的工作 - 迄今为止它不工作
I am trying to get client side validation to work on my model -- so far it's not working.
我的模型,这是一个集合的属性:
My model has a property that's a collection:
public class NewsEventsModel
{
public List<NewsItemDetails> newsItems { get; set; }
public int pageNumber { get; set; }
public int totalPages { get; set; }
public bool canManageNews { get; set; }
public long userID { get; set; }
}
和NewsItemDetails被定义正是如此:
and NewsItemDetails is defined thusly:
public class NewsItemDetails
{
public long itemID { get; set; }
public long postedByID { get; set; }
public DateTime datePosted { get; set; }
[Required(ErrorMessage = "Please enter news or event text")]
[StringLength(100)]
[RegularExpression(RegExpressions.freeTextRestrict)]
public string newsBody { get; set; }
[StringLength(50)]
[RegularExpression(RegExpressions.freeTextRestrict)]
public string newsTitle { get; set; }
}
我送NewsEventsModel到视图。然后,在我看来,我包括
I send NewsEventsModel over to the view. Then in my view I include
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
然后在视图code我有
Then in the view code I have
for (int j = 0; j < Model.newsItems.Count(); j++)
{
/// bunch of stuff
// then:
<div class="editblock">
@Html.TextAreaFor(model => model.newsItems[j].newsBody, new { @class = "formtextem", id = editBoxID, rows = "10", style = "width: 54em" })
@Html.ValidationMessageFor(model => model.newsItems[j].newsBody)
</div>
}
但是,当我删除文本区域中的所有文本,然后点击保存,该要求消息未出现..也不当我违反任何其他规则。任何消息出现
But when I delete all the text in the text area and click save, the "required" message does not appear .. nor do any messages appear when I violate any of the other rules.
但是,客户端验证确实工作,如果我把验证标注在顶级NewsEventsModel的属性之一,然后把它作为视图中的文本框。它只是不为下级NewsItemDetails类的工作。
However, the client-side validation does work if I put validation annotation on one of the properties in the top-level NewsEventsModel, and then include it as a textbox in the view. It just doesn't work for the lower-level NewsItemDetails class.
我是什么在这里失踪?
推荐答案
尝试在视图中使用编辑器模板:
Try using Editor Templates in your view:
@Html.EditorFor(x => x.newsItems)
和里面的〜/查看/共享/ EditorTemplates / NewsItemDetails.cshtml
:
@model AppName.Models.NewsItemDetails
// bunch of stuff
// then:
<div class="editblock">
@Html.TextAreaFor(model => model.newsBody, new { @class = "formtextem", id = editBoxID, rows = "10", style = "width: 54em" })
@Html.ValidationMessageFor(model => model.newsBody)
</div>
这篇关于关于收集MVC 3客户端验证与数据的注解 - 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!