I wanted to give a quick but clear example how to use jQuery to update a partial view, an example which could be easy to replicate.
We need a model
The model consists of product class with three properties; a product code, name and price.
public class Product
{
[Required]
public string Code { get; set; }
[Required]
public string Name { get; set; }
[Required]
[DataType(DataType.Currency)]
public decimal UnitPrice { get; set; }
}
And a Controller
For simplicity there is no specific data layer or repository, the controller contains a global array of products. When a http post is made to the GetProducts ActionResult a parameter is passed specifying how many products to return.
public class HomeController : Controller
{
private List<Product> products = new List<Product>() ;
public HomeController()
{
products.Add(new Product { Code = "P001", Name = "Hair Dryer", UnitPrice = 10.50M });
products.Add(new Product { Code = "P002", Name = "Carpet Cleaner", UnitPrice = 3.60M });
products.Add(new Product { Code = "P003", Name = "Hair Remover", UnitPrice = 12.10M });
products.Add(new Product { Code = "P004", Name = "Vacuum Cleaner", UnitPrice = 80.99M });
products.Add(new Product { Code = "P005", Name = "Table", UnitPrice = 7.69M });
products.Add(new Product { Code = "P006", Name = "Duvet Cover", UnitPrice = 12.89M });
products.Add(new Product { Code = "P007", Name = "Towel", UnitPrice = 3.50M });
products.Add(new Product { Code = "P008", Name = "Electric Oven", UnitPrice = 125.99M });
products.Add(new Product { Code = "P009", Name = "Hair Straighteners", UnitPrice = 8.90M });
products.Add(new Product { Code = "P010", Name = "Belt", UnitPrice = 3.50M });
}
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult GetProducts(int quantity)
{
var selectedProducts = products.Take(quantity);
return PartialView("_ProductList", selectedProducts);
}
}
The Main View

Selecting from the dropdown list causes a change event the initiates the post to the server. If the post is successful the partial view is updated.
The date and time is displayed for comparison with the date and time on the partial view, they will be different.
<h2>Product Display</h2>
<div class="col-sm-6">
@DateTime.Now.ToLongDateString()
@DateTime.Now.ToLongTimeString()
<div class="panel panel-default">
<div class="panel-heading">
Products</div>
<div class="panel-body">
<div class="form-group form-group-sm">
<label class="control-label" for="PageSize">
How many products should be displayed?
</label>
<div>
<select id="PageSize">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></div>
</div>
<div id="SelectedProducts"></div>
</div>
</div>
</div>
jQuery AJAX
When a selection is made from the dropdown list the change event is triggered and a POST made to the server.
@section scripts{
<script type="text/javascript">
$(document).ready(function () {
$("#PageSize").on("change", function () {
var val = $('#PageSize').val();
$.ajax({
url: "/Home/GetProducts",
type: "POST",
data: { quantity: val }
})
.done(function (partialView) {
$("#SelectedProducts").html(partialView);
});
});
});
</script>
}
And the Partial View (_ProductList.cshtml)
The partial view displays a list of products in a table. The POST date and time is displayed for comparison purposes.
@model IEnumerable<MVCUpdatePartial.Models.Product>
<div class="editor-label">
Selected Products</div>
<table class="table">
@foreach(var product in Model)
{
<tr>
<td>@product.Code</td>
<td>@product.Name</td>
<td>@product.UnitPrice</td>
</tr>
}</table>
Updated: @DateTime.Now.ToLongDateString()
@DateTime.Now.ToLongTimeString()
After the POST
And how the view looks after the POST and the partial view is updated.


One thought on “How to update partial view using jquery ajax”