Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display name on model not being displayed?

Tags:

c#

asp.net-mvc

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?

like image 747
Kyle Avatar asked Jun 20 '12 16:06

Kyle


1 Answers

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.

like image 146
nemesv Avatar answered Sep 21 '22 05:09

nemesv