我可以在 MVC2 Html Helper 中获取属性的包含模型吗
我有一个具有以下签名的 Html 帮助程序:
public static MvcHtmlString UiAutoCompleteForWithId<TModel, TProperty>(this htmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
我了解如何获取传入成员的值及其元数据,但是无论如何都可以访问包含的模型,或更具体地说是特定同级成员的值(按名称传入的成员的属性)?
干杯, 马修
编辑:好的,我想我可以使用 ModelMetadata.FromStringExpression 方法来做到这一点(有时需要先询问才能看到它,呃?),但这是最好的方法吗?
I have an Html helper with the following signature:
public static MvcHtmlString UiAutoCompleteForWithId<TModel, TProperty>(this htmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
I understand how to get my hands on the value of the passed in member and it's metadata, but is there anyway to access the containing Model, or more specifically the value of a particular sibling member (a property) of the passed in member by name?
Cheers,
Matthew
Edit: OK I think I can do this using the ModelMetadata.FromStringExpression method (sometimes it takes asking before you see it eh?), but I is this the best way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要访问同级成员的值,则意味着您假设视图模型具有该同级成员。这意味着您的 html 助手不再需要是通用的。您可以这样做:
或者,如果您的视图模型实现了一些包含相关同级成员的通用基本接口,您可以指定一个通用约束:
If you need to access the value of a sibling member it means that you assume that the view model has this sibling member. This means that your html helper no longer needs to be generic. You could do this:
Or if your view models implement some common base interface that contains the sibling member in question you could specify a generic constraint: