C# 为什么不显示显示名称?

C# 为什么不显示显示名称?,c#,asp.net-mvc,visual-studio,asp.net-mvc-4,razor,C#,Asp.net Mvc,Visual Studio,Asp.net Mvc 4,Razor,我试图在ASP.NET MVC 4应用程序中显示html中属性的显示名称。 然而,即使我在viewmodel中设置了displaynames,原始变量名仍然会显示出来 viewmodel: public class Manage_ManagePasswordViewModel { [Display(Name="Name")] public string name; [Display(Name = "SubID")] public string subId;

我试图在ASP.NET MVC 4应用程序中显示html中属性的显示名称。 然而,即使我在viewmodel中设置了displaynames,原始变量名仍然会显示出来

viewmodel:

public class Manage_ManagePasswordViewModel
{
    [Display(Name="Name")]
    public string name;
    [Display(Name = "SubID")]
    public string subId;
    [Display(Name = "Conversions")]
    public int conversions;
    [Display(Name = "Password")]
    public string password;
    public UserProfile owner;

    public Manage_ManagePasswordViewModel(ProtectedPassword pw)
    {
        name = pw.Name;
        subId = pw.SubId;
        conversions = pw.GetConversions();
        password = pw.Password;
        owner = pw.Owner;
    }
}
cshtml文件:

@model Areas.Admin.Models.ViewModels.Manage_ManagePasswordViewModel

<div class="editor-label">
    @Html.DisplayNameFor(m => m.name):
    @Html.DisplayTextFor(m => m.name)
</div>
<div class="editor-label">
    @Html.DisplayNameFor(m => m.password):
    @Html.DisplayTextFor(m => m.password)
</div>
<div class="editor-label">
    @Html.DisplayNameFor(m => m.subId):
    @Html.DisplayTextFor(m => m.subId)
</div>
<div class="editor-label">
    @Html.DisplayNameFor(m => m.conversions):
    @Html.DisplayTextFor(m => m.conversions)
</div>
<div class="editor-label">
    @Html.DisplayNameFor(m => m.owner.UserName):
    @Html.UserLink(Model.owner.UserName, Model.owner.UserId)
</div>
@model Areas.Admin.Models.ViewModels.Manage\u ManagePasswordViewModel
@Html.DisplayNameFor(m=>m.name):
@DisplayTextFor(m=>m.name)
@Html.DisplayNameFor(m=>m.password):
@DisplayTextFor(m=>m.password)
@Html.DisplayNameFor(m=>m.subId):
@DisplayTextFor(m=>m.subId)
@Html.DisplayNameFor(m=>m.conversions):
@DisplayTextFor(m=>m.conversions)
@Html.DisplayNameFor(m=>m.owner.UserName):
@UserLink(Model.owner.UserName、Model.owner.UserId)

例如,使用属性而不是字段

[Display(Name="Name")]
public string name {get;set;}

DisplayName
LabelFor
(我说得对吗?)@wudzik根据,它是。
@Html.LabelFor(m=>m.name)
工作吗?不确定是否有任何区别,但这些字段不是属性。@Henk Mollema LabelFor给出了相同的原始变量名-相同的结果。非常感谢解决了这个问题。介意解释一下为什么我需要使用属性吗?也许,这只是MVC开发团队使用的常见约定(就像MVC中的许多其他功能一样,例如控制器命名)。在MVC系统中使用.NETFramework反射,可以轻松地检查属性和填充绑定模型。但这是我的观点,我不是MVC内部的大师)