为什么这不显示显示名称?

时间:2013-07-25 10:35:46

标签: c# asp.net-mvc visual-studio asp.net-mvc-4 razor

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

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>

1 个答案:

答案 0 :(得分:2)

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

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