Introduction
This article is based on the Basics of MVC3 (Part-1), so please read Part-1
before reading this article. This article will explain how to create an MVC3
usercontrol using RenderAction, PartialView, and jQuery’s .ajax()
method.
RenderAction
Invokes a child action method and renders the result inline in the parent view.
PartialView
Creates a PartialViewResult
object that renders a partial view, it helps to render part of the view with a specified view.
jQuery’s .ajax()
Performs an asynchronous HTTP (AJAX) request and prevents the postback operation.
Refactor ProductController
Step 1
Create the ProductList
method and return a list of ProductModel
s using PartialView
.
public virtual ActionResult ProductList()
{
return PartialView("ProductList", AllProducts);
}
Similarly create the ProductDetail
method and return ProductModel
using PartialView
.
public virtual ActionResult ProductDetail(string strPrdCode)
{
int prdCode;
if (!int.TryParse(strPrdCode, out prdCode))
prdCode = 0;
if (prdCode == 0)
prdCode = AllProducts[0].PrdCode;
var prd = model.GetAProduct(prdCode);
return PartialView("ProductDetail", prd);
}
These code snippets wre extracted from the ProductController.cs Index
method, now the Index
method returns an empty view.
public ActionResult Index()
{
return View();
}
ProductList PartialView
Step 2
Right click the ProductList
method in the ProductController
class and click the Add View menu.
It will open the Add View window, in that select Create as a partial view checkbox and choose all the fields as in the screen below.
It will create the empty ProductList.cshtml view under views/Product folder.
Step 3
Copy the product list HTML code from index.cshtml (from Part 1) and paste in the ProductList.cshtml file and change the action link section as below.
<span id="@p.PrdCode" style="text-decoration:underline; color:Blue; cursor:hand;"
onclick="javascript:void onLinkClick(@p.PrdCode)" >
@p.Name
</span>
For detailed code, please refer the attached code zip.
ProductDetail PartialView
Step 4
Similar to the ProductList
view, create the ProductDetail
view by right clicking the ProductDetail
method in ProductController.css.
Step 5
Copy the product details section HTML code from index.cshtml and paste in the ProductDetail.cshtml file.
Create the .ajax() method for the POST operation
For doing the asynchronous HTTP request action, we can use the .ajax()
method. The JavaScript code below calls the HTTP POST request when clicking on the Product List link.
<script language="javascript" type="text/javascript">
function onLinkClick(strPrdCode) {
var product = { 'strPrdCode': strPrdCode };
$.ajax({
type: "POST",
cache: false,
url: 'Product/ProductDetail',
data: product,
success: function (data, xhr, settings) {
$('#ProductDetailDiv').html(data);
},
error: function (xhr, textStatus, error) {
$('#ProductDetailDiv').html(textStatus);
}
});
}
</script>
Refactor Index.cshtml
Index.cshtml HTML code is moved to ProductList.cshtml and ProductDetail.cshtml and
the RenderAction
HTML helper class is added for calling these views.
<div style="width: 100%">
<div id="ProductListDiv">
@{Html.RenderAction("ProductList");}
</div>
<div id="ProductDetailDiv">
@{Html.RenderAction("ProductDetail");}
</div>
</div>
Build and Run
Build and run the project, the UI has a similar look and feel as the Part-1 demo, but when clicking the Product link in the Product list section, the page postback action
does not happen in Part-2 code.
Conclusion
I hope this article helps you understand the basics of jQuery’s .ajax()
method with RenderAction
and PartialView
concepts.
References:
- http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.renderaction.aspx
- http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.partialview.aspx
- http://api.jquery.com/jQuery.ajax/