史蒂夫·桑德森(Steve Sanderson)的BeginCollectionItem并非在所有情况下都有效...潜在的解决方案? [英] Steve Sanderson's BeginCollectionItem not working in all cases... potential solution?

查看:70
本文介绍了史蒂夫·桑德森(Steve Sanderson)的BeginCollectionItem并非在所有情况下都有效...潜在的解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用史蒂夫·桑德森(Steve Sanderson)的 BeginCollectionItem 实用程序来呈现要在MVC3中编辑的对象的列表,当您从迭代器呈现整个集合时,此功能非常有用.当我试图将一个新项添加到集合中并返回表示该对象的html时,我的问题就来了.出于某种原因,我的数据注释未在源于代码的html中呈现.

I'm working with Steve Sanderson's BeginCollectionItem utility to render a list of objects to be edited in MVC3, and it works great when you're rendering an entire collection from an iterator. My problem is coming when I'm trying to just add one new item to the collection, and return the html that represents that object. For some reason, my data annotations aren't being rendered in the html coming down from code.

是否有任何可用的修复程序,或者有什么不同之处,除非必须手动编写验证才能解决此问题?

Is there any fix available to this, or is there anything different, sans having to write the validation by-hand, that I can do to solve this issue?

谢谢.

推荐答案

要考虑的事情:

    除非使用任何用于创建此附加对象的方法中存在FormContext,否则不会呈现
  1. 数据注释.如果您使用的是局部视图,请在顶部添加以下内容:
  1. Data annotations will not be rendered unless a FormContext exists in whatever method you are using to create this additional object. If you are using a partial view, add the following to it at the top:

-

   if (this.ViewContext.FormContext == null) 
   {
       this.ViewContext.FormContext = new FormContext(); 
   } 

  1. 如果要通过AJAX向页面动态添加项目,则在添加新项目之后,必须清除DOM中的验证数据,然后重新解析所有验证元素,如下所示:

-

   $("form").removeData("validator");
   $("form").removeData("unobtrusiveValidation");
   $.validator.unobtrusive.parse("form");

这篇关于史蒂夫·桑德森(Steve Sanderson)的BeginCollectionItem并非在所有情况下都有效...潜在的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆