简体   繁体   中英

Returning an IEnumerable<> list to a partial view from a view

I have a project I am working on for a Meal Planner. I have a simple product model and a Meal model which should be a collection of products.

I am trying to create a "Create" view in which I can enter the name of the meal and I thought that I would return a partial view that would have a list of available products to choose from.

I know you can send a model to the partial view using

@Html.Partial("_ProductList", new MealPlanner.Models.Product())

My PartialView takes a

@model IEnumerable<MealPlanner.Models.Product>

This obviously throws an error so how would I pass a List of Products to my partial?

Ok so @scartag put me on the right track but I started looking at ViewModels and came up with the following...

public class ViewModelProducts
{
    public Meal Meal { get; set; }
    public virtual IEnumerable<Product> Products { get; set; }
}

This allows me to pass in a everything I need for the create view and then I can send the list of products to the partial as follows...

@Html.Partial("_ProductList", Model.Products)

Then all I had to do was to customize my partial view. Thanks for the quick relies!

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