自定义数据注释和视图模型
我编写了一个自定义的 DataAnnotation,它工作得很好,除非我使用 ViewModel 而不是实际的模型本身。
使用模型时,数据注释会呈现以下效果:
<label class="tooltip" for="title">Enter a title.</label>
当我使用 ViewModel 时,数据注释仍然呈现相同的内容。这里的问题是“for”应该是“Product_title”而不是“title”。
这是我编写的 HTMLHelper,它从数据注释中呈现标签:(
取自 asp.net MVC 扩展DataAnnotions)
public static MvcHtmlString TooltipFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) {
var exp = (MemberExpression)expression.Body;
foreach (Attribute attribute in exp.Expression.Type.GetProperty(exp.Member.Name).GetCustomAttributes(false)) {
if (typeof(Tooltip) == attribute.GetType()) {
return MvcHtmlString.Create("<label class=\"tooltip\" for=\"" + exp.Member.Name + "\">" + ((Tooltip)attribute).Description + "</label>");
}
}
return MvcHtmlString.Create("");
}
我在表达式对象中进行了深入研究,以找出哪个属性保存模型属性名称,该名称最终将成为输入 ID; “exp.会员.姓名”。
现在我使用的是 ViewModel,显然我需要一些不同的东西,因为“exp.Member.Name”仍然返回“title”而不是“Product_title”,它最终是输入字段的 ID。
那么,是否有一个我可以使用的属性来代替“Member.Name”,它总是返回正确的 ID,或者我是否必须使用 JavaScript 来解决这个问题(如果其他一切都失败,我可以这样做) ?
I wrote a custom DataAnnotation that works perfectly fine unless I use a ViewModel rather then the actual Model itself.
With a Model, the data annotation renders something to the effect of:
<label class="tooltip" for="title">Enter a title.</label>
When I use a ViewModel, the data annotation is still rendering the same thing. The problem here is the "for" should be something like "Product_title" instead of "title".
Here's the HTMLHelper I wrote that renders the label from the data annotation:
(Taken from asp.net MVC extending DataAnnotions)
public static MvcHtmlString TooltipFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) {
var exp = (MemberExpression)expression.Body;
foreach (Attribute attribute in exp.Expression.Type.GetProperty(exp.Member.Name).GetCustomAttributes(false)) {
if (typeof(Tooltip) == attribute.GetType()) {
return MvcHtmlString.Create("<label class=\"tooltip\" for=\"" + exp.Member.Name + "\">" + ((Tooltip)attribute).Description + "</label>");
}
}
return MvcHtmlString.Create("");
}
I dug around in the expression object to figure out which property holds the model property name which would end up being the input ID; "exp.Member.Name".
Now that I'm using a ViewModel, I apparently need something different, because "exp.Member.Name" is still returning "title" instead of "Product_title", which ends up being the ID of the input field.
So, is there a property I can get at that I can use instead of "Member.Name" that will always return the proper ID, or will I have to get around this issue with JavaScript (which I can do if everything else fails)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近实现了类似的东西,采用显示注释的“描述”属性并将其输出到屏幕:
我将这些帮助器输出到工具提示类型控件。我考虑过实现我自己的工具提示属性,但它似乎超出了我的需要......
I recently implemented something similar, taking the "Description" attribute of the Display annotation and outputting it to the screen:
I output these helpers to tooltip type controls. I considered implementing my own tooltip attribute, but it seemed to be more than I needed...