Jitender Kumar
Jitender Kumar

Reputation: 2587

MetadataType attribute is not working for details view in MVC3

I am newbie in entity framework and I have created Ado.Net Entity Data model which contains two properties FullName and EmailAddress. what I am trying to do is that in my details view I want to display Full Name instead of FullName. To accomplish this I have written code like this:

namespace ScaffFolding.Models
{
 [MetadataType(typeof(EmployeeMetaData))]
 public partial class Employee
 {

 }
 public class EmployeeMetaData
 {
    [Display(Name="Full Name")]
    public string FullName { get; set; }

    [Display(Name = "Email Address")]
    public string EmailAddress { get; set; }
 }
} 

My edmx file also sharing the same namespace and have same class named as Employee
Now the problem is that in Details view this code is not working means showing FullName instead of Full Name but in Create view it's working as expected.

Here is code for my Detail view:

@model ScaffFolding.Models.Employee

@{
ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
<legend>Employee</legend>

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

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

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

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

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

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

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

Can anyone please tell me what is going wrong here?

Upvotes: 0

Views: 673

Answers (2)

user1635354
user1635354

Reputation:

DisplayFor is used for picking up templates from DisplayTemplates folder. more at What is the @Html.DisplayFor syntax for?

Where as, LabelFor picks stuffs from data annotations

Upvotes: 1

Jitender Kumar
Jitender Kumar

Reputation: 2587

Ok I got the answer what is going wrong here. The problem is in the code generated by VS:

Visual Studio is generating the Details view using div tag as shown below

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

Where as LabelFor is being used by Vistual Studio for Create view:

@Html.LabelFor(model => model.FullName)

Upvotes: 0

Related Questions