DataAnnotations on Entity not displaying in View

I have a Supplier Entitiy that contains

ID - int
Status - string
Name - string
CreateDate- datetime

I am using the partial class method to create Data Annotations for the above Entity.as described here

namespace TemplateEx.Models
{
    [MetadataType(typeof(SupplierMetadata))]
    public partial class Supplier
    {
        // Note this class has nothing in it.  It's just here to add the class-level attribute.
    }

    public class SupplierMetadata
    {
        // Name the field the same as EF named the property - "FirstName" for example.
        // Also, the type needs to match.  Basically just redeclare it.
        // Note that this is a field.  I think it can be a property too, but fields definitely should work.

        [Required]
        [Display(Name = "Supplier Name")]
        public string Name;
    }
}

My defined a controller action as below

 public ViewResult Details(int id)
    {
        Supplier supplier = db.Suppliers1.Single(s => s.ID == id);
        return View(supplier);
    }

When I create a view for this action and picked the Details scaffolding for the Supplier entity following is what I got as a view

@model TemplateEx.Models.Supplier

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
    <legend>Supplier</legend>

    <div class="display-label">CreateDate</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CreateDate)
    </div>

    <div class="display-label">Status</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Status)
    </div>

    <div class="display-label">Name</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Name)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

Notice how the model.Name has a "Name" label instead of "Supplier Name".What am I doing wrong?

Answers


replace

<div class="display-label">Name</div>

with

<div class="display-label">@Html.LabelFor(model => model.Name)</div>

Edit :

For the second question, look here How can i enforce the scaffolding automatically generated code to display the Label and the EditorFor text field at the same line in my asp.net mvc 3 (specially last answer)


Need Your Help

showing dynamic text in silverlight

silverlight xaml dynamic-text

I have a button which shows "click me!" on it. I want to see that when I click on the button, it shows "Do not click on me!"

C# inheritance issue

c# inheritance polymorphism

I'm using an API that has a "Member" class. I wish to extend this so i have create "MemberProfile" which inherits from "Member"