[英]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.
我有一个简单的产品模型和一个Meal模型,它应该是一个产品集合。
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 我的PartialView需要一个
@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... 好吧@scartag让我走上了正确的轨道,但我开始关注ViewModels,并想出了以下内容......
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!
谢谢快速的依赖!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.