0

I call my MVC controller action via Ajax, and want the action to return two partial views. Both partial views should be returned as html.

This is how I call via ajax

    $.ajax({
        url: '/SomeController/GetContent',
        type: 'GET',
        dataType: "html",
        success: function (d, status, xhr) {
            $('#partial1') = d.?
            $('#partial2') = d.?
        },
        error: function (req, status, errorObj) {
            alert('error');
        }
    });

I tried something like this for the controller action:

    public PartialViewResult[] GetContent()
    {
        PartialViewResult[] pvs = new PartialViewResult[2];

        pvs[0] = PartialView("Partial1", null);
        pvs[1] = PartialView("Partial2", null);

        return pvs;
    }

It works if I simply return one partial view (not returning as an array), but for two I just get an empty array returned in the ajax success. Any ideas how to solve this?

6
  • 1
    Return a single view that contains the 2 partial views. Commented Mar 13, 2015 at 22:50
  • Thanks. Could you show how this is done? Commented Mar 13, 2015 at 22:52
  • if you send pvs as json array can loop over array and insert into appropriate elements. Adjust dataType accordingly Commented Mar 13, 2015 at 22:55
  • @charlietfl: I don't think that will work, because if I return it as json I will lose the html completely. Commented Mar 13, 2015 at 22:58
  • 1
    example of html in json jsfiddle.net/5ndq8qzv Commented Mar 14, 2015 at 0:10

2 Answers 2

1

Create a single view that returns both partials

/Views/YourController/GetContent.cshtml

@Html.Partial("Partial1")
@Html.Partial("Partial2")

and change the method to

public PartialViewResult GetContent()
{
    return PartialView();
}

However if you need control over where each partial is placed in the view where you calling this method from, then you will need 2 separate methods and 2 separate ajax calls.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but I will need to add the partials manually in my view with JQuery, so will probably have to go with two separate calls then as you suggest.
And since ajax is async, it will probably give better response time anyway.
don't have to make 2 requests, can filter the response by wrapping in $() and using jQuery methods to get the content for each element in DOM
0

I am not sure what you will be going to achieve but you are breaking basic binding of partial view and binding. are those both partial views having same one functionality but separate info then put in single partial view or if you have different functionality with separate view then bind with 2 ajax call. Alternate way you can use latest features of MVC6 viewcomponent http://www.asp.net/vnext/overview/aspnet-vnext/vc or AngularJS state binding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.