简体   繁体   中英

C# MVC4 Razor - can DisplayNameFor be used to get a Display(Name = …) of a different model?

When I have a Model in my View , I can easily access a member's DisplayName by

@Html.DisplayNameFor(m => m.myMember)

But when I need the DisplayName of a member from a different model there seems to be no easy solution, there are some that are digging in the ModelMetaData or System.ComponentModel.DataAnnotations.DisplayAttribute , but these are all some multistep things that are not easy to read or understand.

If you take a look at the definition of DisplayNameFor

DisplayNameFor<TModel, TValue>(
    this HtmlHelper<IEnumerable<TModel>> html,
    Expression<Func<TModel, TValue>> expression
)

It seems that it could be easily used by something like

@Html.DisplayNameFor<MyDifferentModel, string>(?, m => m.memberInMyDifferentModel)

but I don't get the clue about one of the parameters TModel is the model, TValue the type of my member, Expression... the m => m.myMember thingy, but what's that other parameter? Is it possible to use DisplayNameFor in that way? And what are the missing pieces?

Thanks for any suggestions - the examples in the www and on SO all handle just the case where you have the model in the view, nested models in the view, whatever what you can access directly by the model in the view, but that's not what I want.

Edit:

At the moment I use this helper function to get the display name

public string getDisplayName<Type>(string member)
{
    var type = typeof(Type);
    var memInfo = type.GetMember(member);
    var attributes = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false);
    var displayname = ((System.ComponentModel.DataAnnotations.DisplayAttribute)attributes[0]).Name;
        return displayname;
}

Can be easily used with

getDisplayName<MyDifferentModel>("memberInMyDifferentModel");

I just wondered if this functionality is already built in the HTML helper DisplayNameFor ...

Try using:

[DisplayName("Sub-Category Name")]
public string subCategoryName { get; set; }

@Html.DisplayNameFor(model => model.subCategoryName)

After some research ... it seems that you have to create your own Html , so after

var MyHtml = new HtmlHelper<MyDifferentModel>(ViewContext, Html.ViewDataContainer)

in your view (don't know if it's a good idea or a problem to reuse the ViewContext and ViewDataContainer of the already instantiated Html ), you can use

MyHtml.DisplayNameFor(m => m.memberInMyDifferentModel)

to get that display name. Or if you want it in one line ...

(new HtmlHelper<MyDifferentModel>(ViewContext, Html.ViewDataContainer)).DisplayNameFor(m => m.memberInMyDifferentModel)

While this seems to work (and seems to be a more clean solution than digging in annotations), there's something that I don't understand yet: why do you need to create a new Html and can't use the generics in Html.DisplayNameFor<TModel,TValue> directly? Please comment ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM