News

Blog moved to http://blachniet.com

My Stats

  • Posts - 6
  • Comments - 16
  • Trackbacks - 0

Twitter











Recent Comments


Recent Posts


Archives


Post Categories



This blog has been relocated to http://blachniet.com. See this post at it's new home: http://blachniet.com/2011/08/10/partial-views-with-unobtrusive-ajax/.



This is post is meant to be a more straightforward overview of my previous post. The previous post got a little long and turned into a walthrough. If you need a walkthrough of this, check out that post at http://geekswithblogs.net/blachniet/archive/2011/08/03/walkthrough-updating-partial-views-with-unobtrusive-ajax-in-mvc-3.aspx

So in my new exploration of web development, I found what I assume to be a fairly common need to have part of a view/page update without the entire page updating. In my particular case, I wanted to have a page that listed items but also provided a form that allowed you to add an item. When an item was added, the list of items would be updated without having to regenerate the entire page.

This was eventually accomplised through the unobtrusive AJAX provided through the MVC and jQuery frameworks. Below you can see the Razor page that was used to solve my problem. In this page, we include the jquery.unobtrusive-ajax.min.js and create a form using the AjaxHelper. The Ajax form indicates that it needs to call the action function Index_AddItem, and then update the productList when the form is submitted.  At the bottom, in the productList div, we render the partial view, ProductListControl with the list of products. (ProductIndexViewModel contains a Product and an IEnumerable<Product>. See Rachel Appel's post for more information on ViewModels in MVC here: http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications)

@* Views\Product\Index.cshtml *@
@model BoringStore.ViewModels.ProductIndexViewModel
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
 
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
@using (Ajax.BeginForm("Index_AddItem"new AjaxOptions { UpdateTargetId = "productList" }))
{ 
    <div>
        @Html.LabelFor(model => model.NewProduct.Name)
        @Html.EditorFor(model => model.NewProduct.Name)
    </div>
    <div>
        @Html.LabelFor(model => model.NewProduct.Price)
        @Html.EditorFor(model => model.NewProduct.Price)
    </div>
    <div>
        <input type="submit" value="Add Product" />
    </div>
}
 
<div id='productList'>
    @{ Html.RenderPartial("ProductListControl", Model.Products); }
</div>

The ProductListControl.cshtml is a very simple partial view that displays all the product names and prices in a table:

@* Views\Product\ProductListControl.cshtml *@
@model IEnumerable<BoringStore.Models.Product>
<table>
    <!-- Render the table headers. -->
    <tr>
        <th>Name</th>
        <th>Price</th>
    </tr>
    <!-- Render the name and price of each product. -->
    @foreach (var item in Model)
    { 
        <tr>
            <td>@Html.DisplayFor(model => item.Name)</td>
            <td>@Html.DisplayFor(model => item.Price)</td>
        </tr>
    }
</table>

The controller has a GET action for the Index and a POST action for adding the item. The Index_AddItem function is what the Ajax form calls when it is submitted. This function returns a partial view, allowing us to avoid regenerating the entire page.

// Controllers\ProductController.cs
namespace BoringStore.Controllers
{
    public class ProductController : Controller
    {
        public ActionResult Index()
        {
            BoringStoreContext db = new BoringStoreContext();
            ProductIndexViewModel viewModel = new ProductIndexViewModel
            {
                NewProduct = new Product(),
                Products = db.Products
            };
            return View(viewModel);
        }
 
        public ActionResult Index_AddItem(ProductIndexViewModel viewModel)
        {
            BoringStoreContext db = new BoringStoreContext();
            db.Products.Add(viewModel.NewProduct);
            db.SaveChanges();
 
            return PartialView("ProductListControl", db.Products);
        }
    }
}

 

Summary:

The unobtrusive Ajax provided by MVC and jQuery looks like it may be useful in many areas in my future web development. I hope this post helps get some people to this solution faster than I got to it.


Comments

Gravatar # re: Updating Partial Views with Unobtrusive AJAX in MVC 3
Posted by Joe Zanotti on 1/7/2012 12:05 AM
Windows Azure Platform is one of those cloud computing programs that Windows developed. These kind of programs has been thriving because companies need this kind of programs to run it. It (cloud computing programs) are essentials to these companies because of its complex functions. - Joe Zanotti

Gravatar # re: Updating Partial Views with Unobtrusive AJAX in MVC 3
Posted by Adit on 4/5/2012 6:03 PM
is this code working in mvc 2 ? I implement this in my site but when i am going to add new item means call Index_AddItem(ProductIndexViewModel viewModel) function i found that voewModel.Products is null. I am using List<Product> as Products bcoz i am using this as local storage .. And in Partial view i use Html.RenderPartial("ProductListControl", Model);
Gravatar # re: Updating Partial Views with Unobtrusive AJAX in MVC 3
Posted by Brian Lachniet on 4/6/2012 12:13 AM
I've never used MVC 2, so I'm not sure if this would work there. When you create the ProductIndexViewModel do you intialize Products to a new List<Product>?

ProductIndexViewModel viewModel = new ProductIndexViewModel
{
NewProduct = new Product(),
Products = new List<Product>()
};
Gravatar # re: Updating Partial Views with Unobtrusive AJAX in MVC 3
Posted by Abhishek on 4/24/2012 5:32 PM
This is a very good example.

Thank you
Gravatar # re: Updating Partial Views with Unobtrusive AJAX in MVC 3
Posted by Morgan Sundqvist on 5/14/2012 1:55 AM
Did you have any problems with double postbacks?
When I try your example code in MVC 4 I get two inserts in the database.
Gravatar # re: Updating Partial Views with Unobtrusive AJAX in MVC 3
Posted by Richard on 5/20/2012 11:59 AM
Just wanted to say thanks for the example and sharing!
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: