ASP.NET MVC 2:使用[数据注释]引用可以提供 DDL 列表的方法?
将所有数据传递到视图的一种普遍接受的方法是使用单个数据模型,其中引用域模型和下拉列表 (DDL) 等附加数据。
但是,部分视图(视图模板也是如此)仅接收主模型的一部分,无法访问发送到原始视图的模型的根。那么,如果您的 DDL 列表不是静态的,那么部分视图如何获取数据呢?
有没有办法使用[数据注释]来引用可以返回字段的可能值的方法,然后在分部视图的 DDL 中使用它?此方法将存在于存储库中的何处?
链接或 C# 代码示例会非常有帮助。
A generally accepted way to pass all data to a view is to have a single data model with references to both your domain model and additional data for things like drop down lists (DDL).
However, partial views (view templates too) receive only a portion of the main model, not able to access the root of the Model sent to the original view. So if your DDL lists are not static, how do the partial views get the data?
Is there a way using [Data Annotations] to reference a method which could return the possible values of a field, then use this in the partial view's DDL? Where would this method exist, in the repository?
Links or C# code examples would be very helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有内置的数据注释属性可以满足您的要求。
您可以创建自己的属性,其中包含对类型的引用和静态方法的名称,然后您可以通过部分视图的反射来调用该静态方法。
你将这样的方法放在哪里取决于你正在做什么,尽管我仍然认为收集控制器中的所有输入会更好。您始终可以在 ViewData 集合中设置额外的项目并将其传递到您的部分视图中。
There is no built in Data Annotations attribute that could do what you ask.
You could create your own attribute that contains a reference to a Type and the name of a static method that you can then invoke via reflection from your partial view.
Where you would place such a method depends on what you are doing, though I still think that gathering all the inputs in your controller would be better. You can always set extra items in the ViewData collection and pass those into your partial views.