دنبال کننده ها

۱۳۹۶ دی ۱۰, یکشنبه

asp.net mvc - Changing the [DisplayName] for a common attribute based on the controller rendering the view

[ad_1]



I am building an online shopping website in ASP.Net MVC.



I have base model class, called Item which contains all the common attributes for an Item (Price and title):



public class Item

public long ItemId get; set;
public string Title get; set;

[Required]
[Range(0, int.MaxValue)]
[DisplayName("Price ($)")]
public int Price get; set;



Now I ave a ClothesItem which contains Item and extra attributes like this:



public class ClothesItem

public long ClothesItemId get; set;
public long ItemId get; set;
// more attribures...



And I have HouseRentalItem like this:



public class HouseRentalItem 


public long HouseRentalItemId get; set;
public long ItemId get; set;
public short NoOfBedrooms get; set;
// more attributes...



As you notice the display name is Price, for the price attribute: [DisplayName("Price ($)")]



And I am using a shared PartialView to display the common attributes, for all Items like:



@Html.LabelFor(model => model.Price, htmlAttributes: new @class = "" )
@Html.EditorFor(model => model.Price, new htmlAttributes = new @class = "input-text full-input" )


Which displays "Price" correctly.



What I want is to change the DisplayName to "Weekly Rent" for HouseRentalItem and for every other model should use the term "Price". Please note that all the views use the same common PrtialView to display Item.



Any option, suggestion, on how to change display name depending on the controller rendering the view?




[ad_2]

لینک منبع