4

I'm trying to display the displaynames for the properties in my html in my ASP.NET MVC 4 application. However even though I've set the displaynames in the viewmodel the raw variable names still gets displayed instead.

The 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;
    }
}

The cshtml file:

@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>
Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
Jordan Axe
  • 3,823
  • 6
  • 20
  • 27

1 Answers1

3

Use properties instead of fields, for ex.

[Display(Name="Name")]
public string name {get;set;}
Dmytro
  • 1,590
  • 14
  • 14
  • 1
    Thanks a lot that solved it. Mind explaining why I need to use properties? – Jordan Axe Jul 25 '13 at 11:43
  • Probably, this is just common convention (like many other features in MVC, for example, controllers naming), which MVC development team used. Using .net framework reflections later for MVC system it is easy to examine properties and fill bound models. But this is my point of view, I'm not guru of MVC internals) – Dmytro Jul 25 '13 at 12:02