How to use DisplayForModel() to show a list of Customers?

后端 未结 2 872
忘了有多久
忘了有多久 2021-01-04 06:51

Normally we use DisplayForModel or EditorForModel to display and edit a single Customer object, respectively.

How to display a list of Cust

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 07:15

    Assuming you have a collection of customers in your view model

    public class MyViewModel
    {
        public IEnumerable Customers { get; set; }
    }
    

    you could use the following in your view:

    @Html.DisplayFor(x => x.Customers)
    

    and then in the editor/display template (~/Views/Home/DisplayTemplates/Customer.cshtml or ~/Views/Shared/DisplayTemplates/Customer.cshtml):

    @model AppName.Model.Customer
    
    @Model.Name

    The Customer partial will be then rendered for each element of the customers collection of your main model. The important thing is the naming convention: the partial should be situated in a DisplayTemplates subfolder and called the same as the collection type (Customer).

提交回复
热议问题