简体   繁体   中英

MVC One Type, One Page - Two different DisplayFor

I'm building an intranet and the home page is covered in widgets that show lots of different bits of data. I have a Home.cs model with various IEnumerables properties for the data required. I have been using View folder DisplayFor templates to render that data. I now come to a problem where two different lists of Staff need to be shown in two different ways.

I could use a partial view to render one of the lists or perhaps inherit the class and have a different template. Inheritance seems more work that necessary, I was wondering if anyone knows of a specific way MVC was designed to use or perhaps just a prefered solution by anyone?

Just have two different templates, then specify the template to use for each using this overload of DisplayFor :

@Html.DisplayFor(m => m.StaffList, "StaffTemplate1")

@Html.DisplayFor(m => m.StaffList, "StaffTemplate2")

If you want to use different Display templates for each staff list you can either specify the template to use in the DisplayFor call:

@Html.DisplayFor(m => m.StaffListOne, "StaffTemplateOne");
@Html.DisplayFor(m => m.StaffListTwo, "StaffTemplateTwo");

Or you can add a UIHint attribute to the model properties:

[UIHint("StaffTemplateOne")]
public IEnumerable<Staff> StaffListOne { get;set; }

[UIHint("StaffTemplateTwo")]
public IEnumerable<Staff> StaffListTwo { get;set; }

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