DisplayNameFor tag change text

13,365

Solution 1

You can control the displayed text via the [Display] attribute on your model. In your case you'd want to set the attribute on the underlying model for User1:

public class User
{
   [Display(Name = "Submitted By")]
   public string UserName;
}

See https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute.name%28v=vs.110%29.aspx for more details on the DisplayAttribute class.

Solution 2

As long as you want to change the displayed text of a label no need to change your model.. just edit your Razor View like this:

<dt>
    @Html.DisplayName("Submitted By")
</dt>
<dd>
    @Html.DisplayFor(model => model.User1.UserName)
</dd>

OR

<dt>
        Submitted By
 </dt>
 <dd>
    @Html.DisplayFor(model => model.User1.UserName)
 </dd>

Solution 3

Here is one that is working for me.

@Html.LabelFor(model => model.category_type_id, "Category Type", htmlAttributes: new { @class = "control-label col-md-2" })
Share:
13,365
Hasan Mir
Author by

Hasan Mir

Updated on June 24, 2022

Comments

  • Hasan Mir
    Hasan Mir almost 2 years

    I have following code in my Details View of Entity A

    <dt>
        @Html.DisplayNameFor(model => model.User1.UserName)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.User1.UserName)
    </dd>
    

    I want to change their title that is shown in Details View it is showing UserName where i want it to be Submitted by it cant be overloaded because DisplayName For donot have any overloaded function with two params @Html.DisplayFor(model => model.User1.UserName,"Submitted By") Not working any Help

    Here is my Model of Bugs Where i have Fields of Assigned to and submitted by which are related to the users model so i am having problem that "UserName" is shown at the place of column Name where it should show the Names as "Assigned To" and "Submitted By"

        public partial class Bug
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Priority { get; set; }
        public string Severity { get; set; }
        public string Status { get; set; }
        public int AssignedTo { get; set; }
        public int SubmittedBy { get; set; }
        public int ComponentID { get; set; }
        public int ProjectID { get; set; }
        public System.DateTime DueDate { get; set; }
        public Nullable<int> BugNumber { get; set; }
        public string NormalFlow { get; set; }
        public System.DateTime CreationDate { get; set; }
    
        public virtual Component Component { get; set; }
        public virtual Project Project { get; set; }
        public virtual User User { get; set; }
        public virtual User User1 { get; set; }
    }