user2877820
user2877820

Reputation: 307

Displaying partial view after loading it via Ajax in MVC

I want to load data in the background while the user is on the page. After the data is loaded, I want to display it by using a partial View. So I wrote this:

var serviceURL = '/Vm/GetInformation';
$.ajax({
    type: "POST",
    url: serviceURL,
    data: param = "",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: { "id": id },
    //success: resetPartial()
});

In my controller:

public ActionResult GetInformation()
{
    myCode...
    return PartialView("_List", Costumer);
}

So what is the final step to display that _List partial View in my main view which is now filled with data about costumers (where my ajax function is)

Thank you

Upvotes: 0

Views: 961

Answers (2)

user3263194
user3263194

Reputation: 453

Your controller action expects no parameter. So you can remove the below lines from your ajax call.

data: param = "",
data: { "id": id },

If you really have any parameter id in your controller action, use it like

data: { id:id } 

Have a div in the html to hold the content of the partial view. In the success, use like below:

success : function(result) {
  $("#partialViewHolder").html(result);
}

Upvotes: 0

Brian Mains
Brian Mains

Reputation: 50728

All you need to do is replace the old content:

success: function(d) {
 //d should be a string that's the HTML markup
 $("#someparentelementthatsurroundsinnercontent").html(d);
}

Or you can use any of the methods to append content if you want to append to the end of the list. JQuery gives quite a few options to do this.

Upvotes: 1

Related Questions