开发者

how can i render multiple actions in one call?

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 :)


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>


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);
            }
        });
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜