VS2013 MVC - Display name not working on some fields

3.4k Views Asked by At

Just getting started with MVC.

I have a model and have created a metadata class for persistent data annotations. Some of them work and some don't.

ScreenShot

As you can see from the screen shot, the JobNo displayName isn't working, but the VersionRef is. Can't figure out why.

Anyone got any ideas? main difference is that Jobno from a related table

After some more investigation, it seems that for some reason the scaffolded .cshtml contains a literal string for any fields that from related tables.

<div class="form-horizontal">
    <h4>Job</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.JobNo, "JobNo", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("JobNo", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.JobNo, "", new { @class = "text-danger" })
        </div>
    </div>

Is there a raeson for this?

Regards

mark

3

There are 3 best solutions below

0
On BEST ANSWER

After some more investigation, it seems that for some reason the scaffolded .cshtml contains a literal string for any fields that from related tables.

0
On

Have you tried this? [DisplayAttribute(Name="Full Name")] OR [DisplayName("Full Name")]

You can check this link: Example

0
On

According to this:

https://msdn.microsoft.com/en-us/library/hh833698%28v=vs.118%29.aspx

You are using the overload of LabelFor with the labelText parameter.

public static MvcHtmlString LabelFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression,
    string labelText,
    IDictionary<string, Object> htmlAttributes)

labelText Type: System.String The label text to display.

Consider to use:

@Html.LabelFor(model => model.JobNo, 
    new { @class = "control-label col-md-2" })

See https://msdn.microsoft.com/en-us/library/system.web.mvc.html.labelextensions.labelfor%28v=vs.118%29.aspx for more overloads.