asp.net-mvc 如何填充 mvc razor 局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13769707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to populate mvc razor partial view
提问by Alan Fisher
I need to create a view that displays Order Header information and I need to add a patial view that displays a grid of Line Items. The partial view will be strongly typed from a viewmodel. I understand that I should use the html helper @Html.Partial("Path/view"). I have only used controllers up til now to open a view, populating the viewmodel before sending it to the view. Since the partial view is being called form the html helper, I would like to know is what would be the best way to populate the parital view with the model data.
我需要创建一个显示订单标题信息的视图,我需要添加一个显示行项目网格的 patial 视图。局部视图将从视图模型中强类型化。我知道我应该使用 html helper @Html.Partial("Path/view")。到目前为止,我只使用控制器来打开视图,在将其发送到视图之前填充视图模型。由于局部视图是从 html 帮助程序中调用的,我想知道用模型数据填充局部视图的最佳方法是什么。
回答by Charlino
Option 1: Inherit from parent page
选项 1:从父页面继承
By default, any partial view rendered by calling @Html.Partial("PartialViewName")will get the view model passed to the parent view.
默认情况下,任何通过调用渲染的局部视图@Html.Partial("PartialViewName")都会将视图模型传递给父视图。
So if you have:
所以如果你有:
View Model
查看模型
namespace MyNamesapce
{
public OrderInfoViewModel
{
public string OrderTitle { get; set; }
public IEnumerable<OrderItem> OrderItems { get; set; }
}
}
OrderInfo.cshtml
订单信息.cshtml
@model MyNamespace.OrderInfoViewModel
<h1>@Model.OrderTitle</h1>
@Html.Partial("OrderLineItems")
The OrderLineItems page should get a MyNamespace.OrderViewModelpassed to it... so your partial view should look like this:
OrderLineItems 页面应该得到一个MyNamespace.OrderViewModel传递给它......所以你的部分视图应该是这样的:
OrderLineItems.cshtml
订单行项目.cshtml
@model MyNamespace.OrderInfoViewModel
foreach (var orderItem in Model.OrderItems)
{
//Do stuff
}
Option 2: Specify model
选项 2:指定型号
You can use the second parameter to specify the view model to be passed. I.e.
您可以使用第二个参数来指定要传递的视图模型。IE
OrderInfo.cshtml
订单信息.cshtml
@model MyNamespace.OrderInfoViewModel
<h1>@Model.OrderTitle</h1>
@Html.Partial("OrderLineItems", Model.OrderItems)
OrderLineItems.cshtml
订单行项目.cshtml
@model IEnumerable<OrderItem>
foreach (var orderItem in Model)
{
//Do stuff
}
Option 3: Use partial actions
选项 3:使用部分操作
If you need to reuse a partial view over multiple pages, it could be a good idea to use a partial view to eliminate having to populate different view models with the same info just because the page is going to be using the same partial.
如果您需要在多个页面上重用分部视图,最好使用分部视图来消除使用相同信息填充不同视图模型的必要性,因为页面将使用相同的分部。
E.g.
例如
View Model
查看模型
namespace MyNamesapce
{
public OrderInfoViewModel
{
public string OrderTitle { get; set; }
}
}
Controller
控制器
public class OrderController : Controller
{
public ActionResult OrderInfo(int orderId)
{
OrderInfoViewModel viewModel = GetViewModel(orderId);
return View(viewModel);
}
public PartialViewResult OrderLineItems(int orderId)
{
IEnumerable<OrderItem> orderItems = GetOrderItems(orderId);
return Partial(orderItems);
}
}
OrderInfo.cshtml
订单信息.cshtml
@model MyNamespace.OrderInfoViewModel
<h1>@Model.OrderTitle</h1>
@Html.Action("OrderLineItems")
OrderLineItems.cshtml
订单行项目.cshtml
@model IEnumerable<OrderItem>
foreach (var orderItem in Model.OrderItems)
{
//Do stuff
}
回答by naspinski
With a partial view, you are just sending in a Model just like you would with a normal View. For example, if your Model has a property of LineItem objects named 'LineItems' you simply would do this:
使用局部视图,您只是像使用普通视图一样发送模型。例如,如果您的模型具有名为“LineItems”的 LineItem 对象的属性,您只需执行以下操作:
@Html.Partial("_PartialName", Model.LineItems)
Now if your Model does not have that property, you can either add it, or pass it another way, like ViewBag (I prefer a strongly typed method, but that is my opnion:
现在,如果您的模型没有该属性,您可以添加它,或者以另一种方式传递它,例如 ViewBag(我更喜欢强类型方法,但这是我的选择:
@Html.Partial("_PartialName", (List<LineItem>)ViewBag.LineItems)
These are not the only ways, but they are my preferred methods.
这些不是唯一的方法,但它们是我的首选方法。

