如何在一次调用中呈现多个操作?
如何在对特定控制器的一次调用中呈现多个不同的操作? Html.RenderAction() / Html.Action() 仅处理一个控制器和操作。 但是,如果我想在一次调用中在屏幕上呈现不同的视图怎么办?
提前致谢, 萨吉夫
编辑: 再次嗨。
我不确定你是否理解我的问题。 这是cshtml:
<div id="menu">@using (Ajax.ActionLink("click me", "SomeAction","SomeController", new AjaxOptions() { HttpMethod = "POST", OnSuccess = "showMsg", OnFailure = "showError" }))</div>
<div id="div1">bla bla content</div>
....
<div id="div2">bla bla content</div>
这是控制器:
public class SomeController : Controller
{
public ActionResult SomeAction()
{
return View("somethingfordiv1", ModelForDiv1);
return View("somethingfordiv2", ModelForDiv2); //i want also return another view here
}
}
在控制器上的ajax调用中,我想为2个不同的div返回2个不同的视图。
再次感谢 :)
how can i render multiple different actions in one call to a speccific controller?
Html.RenderAction() / Html.Action() only handles one controller&action.
But what if i want in one call to render different views on the screen?
thanks in advance,
Sagiv
EDIT:
Hi again.
I'm not sure you understood my question.
this is the cshtml:
<div id="menu">@using (Ajax.ActionLink("click me", "SomeAction","SomeController", new AjaxOptions() { HttpMethod = "POST", OnSuccess = "showMsg", OnFailure = "showError" }))</div>
<div id="div1">bla bla content</div>
....
<div id="div2">bla bla content</div>
and this is the controller:
public class SomeController : Controller
{
public ActionResult SomeAction()
{
return View("somethingfordiv1", ModelForDiv1);
return View("somethingfordiv2", ModelForDiv2); //i want also return another view here
}
}
in this ajax call on the controller, i want to return 2 different views for 2 different divs.
thanks again :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您可以继续进行的一种方法。您可以将两个视图模型聚合成一个唯一的视图模型,然后让控制器操作返回一个包含 javascript 的视图,该视图会将两个视图结果注入到不同的 div 中。
一如既往,从视图模型开始:
然后是控制器:
然后是相应的
~/Views/Home/Index.cshtml
视图:接下来是
~/Views/Home/SomeAction.cshtml
> 视图:最后是两个
~/Views/Home/Model1.cshtml
和~/Views/Home/Model2.cshtml
视图:以及:
Here's one way you could proceed. You could aggregate the two view models into a unique view model and then have the controller action return a view containing javascript which will inject the two view results into the different divs.
As always start with the view models:
Then a controller:
Then the corresponding
~/Views/Home/Index.cshtml
view:Next the
~/Views/Home/SomeAction.cshtml
view:and finally the two
~/Views/Home/Model1.cshtml
and~/Views/Home/Model2.cshtml
views:and:
如果您想在屏幕上渲染不同的视图,并返回一个代表这些视图数据的模型,那么您可以使用 RenderPartial 并将所需的模型数据部分传递给每个视图。
您还可以使用 viewdata 单独提供此功能。
Html.RenderAction 也可用,但模拟另一个完整请求
对于您的 ajax 请求,您可以从部分视图的渲染中返回 html 块,这可以由 Request.IsAjaxRequest 确定。然后你的 JavaScript 就可以将结果设置到文档中。
这是你的操作
和客户端示例(使用jquery)
If you want to render different views on the screen return a model which represents the data for those views, then you can use RenderPartial and pass the part of the model data required to each view.
You can also use viewdata to separately have this available.
Html.RenderAction is also available but simulates another full request
For your ajax request you can return a html chunk from the rendering of a partial view and this can be determined by Request.IsAjaxRequest. Then your javascript can set the result into the document.
This is in your action
And the client side example (using jquery)