如何在一次调用中呈现多个操作?

发布于 2024-11-07 21:21:28 字数 955 浏览 0 评论 0原文

如何在对特定控制器的一次调用中呈现多个不同的操作? 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦浮鱼 2024-11-14 21:21:28

这是您可以继续进行的一种方法。您可以将两个视图模型聚合成一个唯一的视图模型,然后让控制器操作返回一个包含 javascript 的视图,该视图会将两个视图结果注入到不同的 div 中。

一如既往,从视图模型开始:

public class Model1 { }
public class Model2 { }

public class AggregatedModel
{
    public Model1 Model1 { get; set; }
    public Model2 Model2 { get; set; }
}

然后是控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult SomeAction()
    {
        var model = new AggregatedModel
        {
            Model1 = new Model1(),
            Model2 = new Model2()
        };
        Response.ContentType = "text/javascript";
        return PartialView(model);
    }
}

然后是相应的 ~/Views/Home/Index.cshtml 视图:

<div id="menu">
    @Html.ActionLink("click me", "SomeAction", "Home", new { id = "clickme" })
</div>

<div id="div1">bla bla content</div>
<div id="div2">bla bla content</div>

<script type="text/javascript">
    $('#clickme').click(function () {
        $.getScript(this.href);
        return false;
    });
</script>

接下来是 ~/Views/Home/SomeAction.cshtml > 视图:

@model AggregatedModel
$('#div1').html(@Html.Raw(Json.Encode(Html.Partial("Model1", Model.Model1).ToHtmlString())));
$('#div2').html(@Html.Raw(Json.Encode(Html.Partial("Model2", Model.Model2).ToHtmlString())));

最后是两个 ~/Views/Home/Model1.cshtml~/Views/Home/Model2.cshtml 视图:

@model Model1
<span>This is the contents for model1</span>

以及:

@model Model2
<span>This is the contents for model2</span>

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:

public class Model1 { }
public class Model2 { }

public class AggregatedModel
{
    public Model1 Model1 { get; set; }
    public Model2 Model2 { get; set; }
}

Then a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult SomeAction()
    {
        var model = new AggregatedModel
        {
            Model1 = new Model1(),
            Model2 = new Model2()
        };
        Response.ContentType = "text/javascript";
        return PartialView(model);
    }
}

Then the corresponding ~/Views/Home/Index.cshtml view:

<div id="menu">
    @Html.ActionLink("click me", "SomeAction", "Home", new { id = "clickme" })
</div>

<div id="div1">bla bla content</div>
<div id="div2">bla bla content</div>

<script type="text/javascript">
    $('#clickme').click(function () {
        $.getScript(this.href);
        return false;
    });
</script>

Next the ~/Views/Home/SomeAction.cshtml view:

@model AggregatedModel
$('#div1').html(@Html.Raw(Json.Encode(Html.Partial("Model1", Model.Model1).ToHtmlString())));
$('#div2').html(@Html.Raw(Json.Encode(Html.Partial("Model2", Model.Model2).ToHtmlString())));

and finally the two ~/Views/Home/Model1.cshtml and ~/Views/Home/Model2.cshtml views:

@model Model1
<span>This is the contents for model1</span>

and:

@model Model2
<span>This is the contents for model2</span>
影子的影子 2024-11-14 21:21:28

如果您想在屏幕上渲染不同的视图,并返回一个代表这些视图数据的模型,那么您可以使用 RenderPartial 并将所需的模型数据部分传递给每个视图。

您还可以使用 viewdata 单独提供此功能。

Html.RenderAction 也可用,但模拟另一个完整请求


对于您的 ajax 请求,您可以从部分视图的渲染中返回 html 块,这可以由 Request.IsAjaxRequest 确定。然后你的 JavaScript 就可以将结果设置到文档中。

这是你的操作

if (Request.IsAjaxRequest())
{
    return View("PartialViewName", partialModel);
}
return View("NormalView", normalModel);

和客户端示例(使用jquery)

    function hijack(form) {
        $("div#SearchResults").html("");
        $("div#SearchResults").addClass('loading');

        $.ajax({
            url: form.action,
            type: form.method,
            dataType: "html",
            data: $(form).serialize(),
            success: function(data) {
                $("div#SearchResults").removeClass('loading');
                $("div#SearchResults").html(data);
            }
        });
    }

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

if (Request.IsAjaxRequest())
{
    return View("PartialViewName", partialModel);
}
return View("NormalView", normalModel);

And the client side example (using jquery)

    function hijack(form) {
        $("div#SearchResults").html("");
        $("div#SearchResults").addClass('loading');

        $.ajax({
            url: form.action,
            type: form.method,
            dataType: "html",
            data: $(form).serialize(),
            success: function(data) {
                $("div#SearchResults").removeClass('loading');
                $("div#SearchResults").html(data);
            }
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文