Click here to Skip to main content
15,501,747 members
Articles / Web Development / ASP.NET
Posted 23 Mar 2012


50 bookmarked

Basics of ASP.NET MVC3: Part-II

Rate me:
Please Sign up or sign in to vote.
4.94/5 (9 votes)
23 Mar 2012CPOL2 min read
How to create an MVC3 usercontrol using RenderAction, PartialView, and jQuery’s .ajax() method.


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.


Invokes a child action method and renders the result inline in the parent view.


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 ProductModels 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.

Image 1

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.

Image 2

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)" >

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.

Image 3

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 };

            type: "POST",
            cache: false,
            url: 'Product/ProductDetail',
            data: product,
            success: function (data, xhr, settings) {


            error: function (xhr, textStatus, error) {





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">
    <div id="ProductDetailDiv">

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.


I hope this article helps you understand the basics of jQuery’s .ajax() method with RenderAction and PartialView concepts.




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Technical Lead
India India
Artha is a Technical Lead in Windows Phone, WPF [MVVM, PRISM], ASP.NET [MVC 3.0 & 4.0], C#.NET, VB.NET and ASP.

Windows Phone

Microsoft Developer Network

Published Tools in Visual Studio Galleries

Published Source Codes in CodePlex

Microsoft Virtual Academy Profile

Published Articles in Code Project

Microsoft Certified Professional Developer (MCPD) Microsoft ASP.NET Developer 3.5 (070-564, 070-536, 070-562, 070 315)

Comments and Discussions

Questionany thing to share about window phone game development Pin
imransaharp17-Mar-14 22:39
Memberimransaharp17-Mar-14 22:39 
AnswerRe: any thing to share about window phone game development Pin
Arthanarieaswaran Shanmugaraj17-Mar-14 23:37
MemberArthanarieaswaran Shanmugaraj17-Mar-14 23:37 
GeneralMy vote of 5 Pin
Ashish Tripathi5-Nov-12 4:39
MemberAshish Tripathi5-Nov-12 4:39 
GeneralRe: My vote of 5 Pin
Arthanarieaswaran Shanmugaraj8-Nov-12 19:27
MemberArthanarieaswaran Shanmugaraj8-Nov-12 19:27 
GeneralMy vote of 5 Pin
ssushil971117-Aug-12 0:39
Memberssushil971117-Aug-12 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.