asp.net-mvc asp.net MVC 局部视图控制器动作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1371031/
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
asp.net MVC partial view controller action
提问by yogibear
I'm very new to web app development and I thought I would start with recent technology and so I'm trying to learn asp.net as-well as the MVC framework at once. This is probably a very simple question for you, MVC professionals.
我对 Web 应用程序开发非常陌生,我想我会从最近的技术开始,所以我正在尝试同时学习 asp.net 以及 MVC 框架。对于 MVC 专业人士来说,这可能是一个非常简单的问题。
My question is should a partial view have an associated action, and if so, does this action get invoked whenever a normal page uses RenderPartial()on the partial view?
我的问题是分部视图是否应该有一个关联的动作,如果是这样,每当普通页面RenderPartial()在分部视图上使用时,是否会调用该动作?
回答by tvanfosson
While you can have an action that returns a partial view, you don't need an action to render a partial view. RenderPartial takes the partial view and renders it, using the given model and view data if supplied, into the current (parent) view.
虽然您可以有一个返回分部视图的操作,但您不需要一个操作来呈现分部视图。RenderPartial 获取局部视图并使用给定的模型和视图数据(如果提供)将其渲染到当前(父)视图中。
You might want an action that returns a partial view if you are using AJAX to load/reload part of a page. In that case, returning the full view is not desired since you only want to reload part of the page. In this case you can have the action just return the partial view that corresponds to that section of the page.
如果您使用 AJAX 加载/重新加载页面的一部分,您可能需要一个返回部分视图的操作。在这种情况下,不需要返回完整视图,因为您只想重新加载页面的一部分。在这种情况下,您可以让操作只返回与页面该部分相对应的部分视图。
Standard mechanism
标准机制
Making use of partial view within a normal view (no action needed)
在普通视图中使用局部视图(无需操作)
...some html...
<% Html.RenderPartial( "Partial", Model.PartialModel ); %>
...more html..
Ajax mechanism
Ajax机制
Reloading part of a page via AJAX (note partial is rendered inline in initial page load)
通过 AJAX 重新加载页面的一部分(注意部分在初始页面加载时内联呈现)
...some html...
<div id="partial">
<% Html.RenderPartial( "Partial", Model.PartialModel ); %>
</div>
...more html...
<script type="text/javascript">
$(function() {
$('#someButton').click( function() {
$.ajax({
url: '/controller/action',
data: ...some data for action...,
dataType: 'html',
success: function(data) {
$('#partial').html(data);
},
...
});
});
});
</script>
Controller for AJAX
AJAX 控制器
public ActionResult Action(...)
{
var model = ...
...
if (Request.IsAjaxRequest())
{
return PartialView( "Partial", model.PartialModel );
}
else
{
return View( model );
}
}
回答by Saeid
The accepted answer is completely correct, but I want to add that you can load your partial view using jQuery load. Less configuration needed, if you don't want to consider concurrency.
接受的答案是完全正确的,但我想补充一点,您可以使用 jQuery load 加载部分视图。如果您不想考虑并发,则需要较少的配置。
$("#Your-Container").load("/controller/action/id");
回答by Peter
The answer is no. But sometimes you need some controller action behind a partial view. Then you can create an actionMethod wich returns a partial view. This actionMethod can be called within another view:
答案是不。但有时您需要在局部视图后面进行一些控制器操作。然后你可以创建一个 actionMethod 返回一个局部视图。这个 actionMethod 可以在另一个视图中调用:
@Html.Action("StockWarningsPartial", "Stores")
The actionmethod can look like:
操作方法可以如下所示:
public ActionResult StockWarningsPartial()
{
....
return View("StockWarningsPartial", warnings);
}
and the view 'StockWarningsPartial.cshtml' starts with:
视图 'StockWarningsPartial.cshtml' 以:
@{
Layout = null;
}
to make it not render your surrounding layout again.
使其不再呈现您的周围布局。
回答by Bern
I was able to achieve something similar with this logic.
我能够用这个逻辑实现类似的东西。
Within the .cshtml
在 .cshtml 中
@Html.Action("ActionMethodName", "ControllerName");
Within the controller
控制器内
[Route("some-action")]
public ActionResult ActionMethodName()
{
var someModel = new SomeModel();
...
return PartialView("SomeView.cshtml", someModel);
}
And that's it.
就是这样。
If you need to pass values from the .cshtml to the action method then that is possible to.
如果您需要将值从 .cshtml 传递给 action 方法,那么这是可能的。

