想知道为什么displayName属性为LabelFor忽略上覆盖的属性 [英] Wondering why DisplayName attribute is ignored in LabelFor on an overridden property
问题描述
结果
今天我做了几个&LT时弄糊涂了;%= Html.LabelFor(M = GT; m.MyProperty)%GT;
在ASP.NET MVC 2和使用 [显示名称(显示此代替myProperty的)]
从属性 System.ComponentModel
。
today I got confused when doing a couple of <%=Html.LabelFor(m=>m.MyProperty)%>
in ASP.NET MVC 2 and using the [DisplayName("Show this instead of MyProperty")]
attribute from System.ComponentModel
.
事实证明,当我把属性上覆盖的属性,LabelFor似乎没有注意到它。结果
但是, [必需]
属性工作正常上覆盖的属性,生成的errormessage的实际使用DisplayNameAttribute。
As it turned out, when I put the attribute on an overridden property, LabelFor didn't seem to notice it.
However, the [Required]
attribute works fine on the overridden property, and the generated errormessage actually uses the DisplayNameAttribute.
这是一些简单的例子code,更现实的情况是,我已经从视图模型分开的databasemodel,但为了方便,我想从databasemodel继承,加上仅查看属性和装潢视图模型与UI的属性。
This is some trivial examplecode, the more realistic scenario is that I have a databasemodel separate from the viewmodel, but for convenience, I'd like to inherit from the databasemodel, add View-only properties and decorating the viewmodel with the attributes for the UI.
public class POCOWithoutDataAnnotations
{
public virtual string PleaseOverrideMe { get; set; }
}
public class EditModel : POCOWithoutDataAnnotations
{
[Required]
[DisplayName("This should be as label for please override me!")]
public override string PleaseOverrideMe
{
get { return base.PleaseOverrideMe; }
set { base.PleaseOverrideMe = value; }
}
[Required]
[DisplayName("This property exists only in EditModel")]
public string NonOverriddenProp { get; set; }
}
强类型的ViewPage&LT; EditModel&GT;
包含:
<div class="editor-label">
<%= Html.LabelFor(model => model.PleaseOverrideMe) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.PleaseOverrideMe) %>
<%= Html.ValidationMessageFor(model => model.PleaseOverrideMe) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.NonOverriddenProp) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.NonOverriddenProp) %>
<%= Html.ValidationMessageFor(model => model.NonOverriddenProp) %>
</div>
然后将标签显示为PleaseOverrideMe的(不可以使用DisplayNameAttribute)和该酒店只有EditModel存在的(< STRONG>使用的DisplayNameAttribute)浏览页面时。结果
如果我发布具有空值,这种ActionMethod触发验证:
The labels are then displayed as "PleaseOverrideMe" (not using the DisplayNameAttribute) and "This property exists only in EditModel" (using the DisplayNameAttribute) when viewing the page.
If I post with empty values, triggering the validation with this ActionMethod:
[HttpPost]
public ActionResult Edit(EditModel model)
{
if (!ModelState.IsValid)
return View(model);
return View("Thanks");
}
在&LT;%= Html.ValidationMessageFor(型号=&GT; model.PleaseOverrideMe)%GT;
实际上使用 [DisplayName的(这应该作为标签,请忽略我!)]
属性,并产生默认ERRORTEXT的的这应该是作为标签,请忽略我!是必填字段。的
the <%= Html.ValidationMessageFor(model => model.PleaseOverrideMe) %>
actually uses [DisplayName("This should be as label for please override me!")]
attribute, and produces the default errortext "The This should be as label for please override me! field is required."
请问一些友好的灵魂阐明这一些轻?
Would some friendly soul shed some light on this?
推荐答案
<一个href=\"https://connect.microsoft.com/VisualStudio/feedback/details/483001/in-asp-net-mvc-generic-try-updatemodel-methods-ignore-subtype-properties\">Model结合和使用强类型助手元数据着眼于声明,而不是运行时,该模型的类型。我认为这是一个错误,但显然MVC开发团队与我不同意,因为我对这个问题的连接被关闭为由设计。
Model binding and metadata using the strongly-typed helpers looks at the declared, rather than the runtime, type of the model. I consider this a bug, but apparently the MVC team disagrees with me, as my Connect issue on this was closed as "By Design."
这篇关于想知道为什么displayName属性为LabelFor忽略上覆盖的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!