[英]Why is the Display(Name = "") attribute ignored in one of my ViewModels while it seems to work in my other models in ASP.NET MVC5?
I have a viewmodel BeerNamePartialVM which has a property with a [Display(Name = "")] attribute that doesn't seem to work.我有一个视图模型 BeerNamePartialVM,它的 [Display(Name = "")] 属性似乎不起作用。 I've also included my base model which also has a property with a [Display(Name = "")] attribute, which does work:我还包含了我的基本模型,它也有一个 [Display(Name = "")] 属性的属性,它确实有效:
public class BeerNamePartialViewModel : IPartialPropertySearch
{
[Required(ErrorMessage = "This field is required")]
[Display(Name = "Name or partial name")]
//[DisplayName("Name or partial name")]
public string PartialName { get; set; }
...
}
public class BierProperties
{
[Required, StringLength(50), Display(Name = "Name")]
public string Naam { get; set; }
...
}
The two highlighted texts are generated with DisplayNameFor HtmlHelper functions shown below:两个突出显示的文本是使用 DisplayNameFor HtmlHelper 函数生成的,如下所示:
@model MVCBierApplication.Interfaces.IPartialPropertySearch
@Html.DisplayNameFor(f => f.PartialName)
@Html.EditorFor(f => f.PartialName)
@Html.ValidationMessageFor(f => f.PartialName)
...
@{Beer emptyModel = new Bier();}
@if (Model.Results != null && Model.Results.Count() != 0)
{
<thead>
<tr>
<td>@Html.DisplayNameFor(h => Model.emptyModel.Naam</td>
...
</tr>
...
the emptyModel is an empty model of type Beer, which I use to build the table header. emptyModel 是 Beer 类型的空模型,我用它来构建表头。 This was neccecary because my view has the interface IPartialPropertySearch
as its model, which returns the results in an IEnumerable of type Object instead of Beer.这是必要的,因为我的视图将接口IPartialPropertySearch
作为它的模型,它在类型为 Object 而不是 Beer 的 IEnumerable 中返回结果。 This was done because I want to reuse my code for models other than Beer.这样做是因为我想将我的代码重用于 Beer 以外的模型。
The result looks like this (this is my first question so I didn't have enough reputation to display the images in-post):结果看起来像这样(这是我的第一个问题,所以我没有足够的声誉来显示帖子中的图像):
Notice the highlighted labels.请注意突出显示的标签。 The label above the textbox seems to ignore it's property's [Display] attribute, while the label in the table header works as expected.文本框上方的标签似乎忽略了其属性的 [Display] 属性,而表头中的标签按预期工作。
I've also tried to use the [DisplayName("name")], first on its own and then even together with [Display(Name =)] but neither seem to work.我也尝试使用 [DisplayName("name")],首先单独使用,然后甚至与 [Display(Name =)] 一起使用,但似乎都不起作用。 I've restarted my IIS server, cleared my browser's cache and ran the app in four different browsers, all of which give the same result.我重新启动了 IIS 服务器,清除了浏览器的缓存并在四个不同的浏览器中运行了该应用程序,所有这些都给出了相同的结果。
The [Required] attribute does work, when I submit the form without entering a name, the errormessage set in the [required] tag gets shown. [Required] 属性确实有效,当我提交表单而不输入名称时,会显示 [required] 标记中设置的错误消息。 The only properties that are getting ignored are those that alter the name:唯一被忽略的属性是更改名称的属性:
I've also included a link to the project's repository in case my images weren't clear enough or if you want to test this yourself.我还包含了指向项目存储库的链接,以防我的图像不够清晰或者您想自己测试。 Some parts might be in Dutch but I think it should be clear enough.有些部分可能是荷兰语,但我认为应该足够清楚。 There's also an SQL file included in the repository to recreate the database I'm using.存储库中还包含一个 SQL 文件,用于重新创建我正在使用的数据库。
I think this is because your model is referencing the interface and not the class.我认为这是因为您的模型引用了接口而不是类。 @model MVCBierApplication.Interfaces.IPartialPropertySearch
change it to @model MVCBierApplication.Models.BeerNamePartialViewModel
@model MVCBierApplication.Interfaces.IPartialPropertySearch
将其更改为@model MVCBierApplication.Models.BeerNamePartialViewModel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.