asp.net-mvc MVC Html.Partial 或 Html.Action
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11766554/
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
MVC Html.Partial or Html.Action
提问by developer747
I am new to asp.net MVC so please bear with me. I need build a menu that repeats across multiple views. What would better serve the purpose Html.ActionOR Html.Partial.
我是 ASP.NET MVC 的新手,所以请耐心等待。我需要构建一个在多个视图中重复的菜单。什么能更好地达到目的Html.ActionOR Html.Partial.
回答by Tommy
Here are what I consider my guidelines on using Html.Action or Html.Partial
以下是我在使用 Html.Action 或 Html.Partial 时考虑的指南
Html.Partial
html.部分
- Use
Html.Partialwhen you are rendering static content or, - If you are going to pass data from the ViewModel that is being sent to the main view
Html.Partial在渲染静态内容时使用,或者,- 如果您要从发送到主视图的 ViewModel 传递数据
Html.Action
动作
- Use
Html.Actionwhen you actually need to retrieve additional data from the server to populate the partial view
- 使用
Html.Action时,你实际上需要从服务器获取额外的数据来填充局部视图
Basically, if is static, use Html.Partial(). If dynamic, model independent data, use Html.Action(). There are probably more scenarios, but this will give you a good idea of where/how to go. Html.RenderPartial()and Html.RenderAction()are interchangeable for the similarly named functions above.
基本上,如果是静态的,请使用Html.Partial(). 如果是动态的、模型独立的数据,请使用Html.Action(). 可能还有更多场景,但这会让您对去哪里/如何去有一个很好的了解。 Html.RenderPartial()并且Html.RenderAction()对于上述类似命名的函数可以互换。
回答by Jesse Hallam
Html.Partial:Render a Partial Viewwithout hitting a controller action first. See: Partial Views
Html.Partial:在不首先点击控制器动作的情况下渲染局部视图。请参阅:部分视图
Html.ActionCall a Controller Action, which may return a view/partial view(or may not, it could throw an HttpNotFound or return Json, or other things).
Html.Action调用一个Controller Action,它可能返回一个视图/部分视图(也可能不会,它可能抛出一个 HttpNotFound 或返回 Json 或其他东西)。
Does your menu require a view model, or is it static?
您的菜单是否需要视图模型,还是静态的?
If it's a static menu, Html.Partialwill fit your needs. Place the static menu content inside the partial view and call Html.Partialwhere you want it to render.
如果它是静态菜单,Html.Partial将满足您的需求。将静态菜单内容放置在局部视图中并调用Html.Partial您希望它呈现的位置。
If the menu is being generated off a view model, you can use eitherHtml.Partialor Html.Action:
如果正在关闭视图模型生成的菜单,你可以使用两种Html.Partial或Html.Action:
Chaining the view model using Html.Partial:
使用 Html.Partial 链接视图模型:
public class MenuViewModel {
// menu data goes here
}
public class GeneralViewModel : MenuViewModel {
// general view info goes here
}
public ActionResult Index() {
return View(new GeneralViewModel());
}
// View Code
@model GeneralViewModel
<div>@Html.Partial("_partialName", model)</div>
Here we pass a complete view model to the view and the view calls Partial and hands its model off to the partial view.
在这里,我们将一个完整的视图模型传递给视图,视图调用 Partial 并将其模型交给局部视图。
** Separating the model using Html.Action:**
** 使用 Html.Action 分离模型:**
public ActionResult Index() {
return View(new GeneralViewModel());
}
public ActionResult MenuView() {
return PartialView(new MenuViewModel());
}
// View Code
@model GeneralViewModel
<div>@Html.Action("MenuView")</div>
Here the view calls the controller action MenuViewwhich creates a new view model and passes it to the partial.
这里视图调用控制器操作MenuView,它创建一个新的视图模型并将其传递给部分。

