First we take this partial view
   1: @using SampleApplication.Models
   2: @model PartialModel
   3:  
4: <div>
   5:     @Model.Text
6: </div>
7: <input type="button" value="click me" onclick="UpdateView();return false;"/>
1: <div id="UpdateableContent">
   2:     @{ Html.RenderPartial("Partial",new PartialModel{Text = "Before Clicked"}); }
3: </div>
   1: public ActionResult UpdatePartial()
   2: {
   3:     var model = new PartialModel() {Text = "was updated"};
   4:     return View("Partial", model);
   5: }
Last we have the Javascript that makes the Ajax call
   1: function UpdateView() {
   2:     var model = { };
   3:     $.post("/home/UpdatePartial",
   4:         model,
   5:         function (data) {
   6:             $("#UpdateableContent").html(data);
   7:         });
   8: }
As always here is the source code for a sample project using it
 
 
 
1 comment:
This is about as clean and simple as it gets. Thanks for the post - I was pulling my hair out and it turns out my only mistake was calling $('#containerDiv').innerHTML = data; instead of .html(data).
Cheers,
Jason
Post a Comment