Kyle
Kyle

Reputation: 17677

Display name on model not being displayed?

I have a model class similar to the following:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

[System.Runtime.Serialization.DataContract(IsReference = true)]
[System.ComponentModel.DataAnnotations.ScaffoldTable(true)]
public class TestModel
{
    [Display(Name="Schedule Name")]
    [Required]
    public string scheduleName;
}

And in my .cshtml file I have:

        <li>
            @Html.LabelFor(m => m.scheduleName)
            @Html.TextBoxFor(m => m.scheduleName, Model.scheduleName)
            @Html.ValidationMessageFor(m => m.scheduleName)
        </li>

But for some reason my display name is not showing up (the label shows 'scheduleName')

I swear I have the same code in other classes and it seems to display just fine. Can anyone point out a reason why this would not work?

Upvotes: 8

Views: 9420

Answers (1)

nemesv
nemesv

Reputation: 139748

The DataAnnotationsModelMetadataProvider works on properties. Your scheduleName should be a property not "just" a field.

[System.Runtime.Serialization.DataContract(IsReference = true)]
[System.ComponentModel.DataAnnotations.ScaffoldTable(true)]
public class TestModel
{
    [Display(Name="Schedule Name")]
    [Required]
    public string scheduleName { get; set; }
}

Note: According to the C# naming conventions your property names should be PascalCased.

Upvotes: 10

Related Questions