Click here to Skip to main content
15,885,244 members
Articles / Web Development / ASP.NET

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 79.2K   2.7K   50   5
How to create an MVC3 usercontrol using RenderAction, PartialView, and jQuery’s .ajax() method.

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

C#
public virtual ActionResult ProductList()
{
    return PartialView("ProductList", AllProducts);
}

Similarly create the ProductDetail method and return ProductModel using PartialView.

C#
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.

C#
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.

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

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.

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

XML
<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:

  1. http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.renderaction.aspx
  2. http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.partialview.aspx
  3. http://api.jquery.com/jQuery.ajax/

License

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 21:39
imransaharp17-Mar-14 21:39 
any thing to share about window phone game development
AnswerRe: any thing to share about window phone game development Pin
Arthanarieaswaran Shanmugaraj17-Mar-14 22:37
Arthanarieaswaran Shanmugaraj17-Mar-14 22:37 
GeneralMy vote of 5 Pin
Ashish Tripathi5-Nov-12 3:39
Ashish Tripathi5-Nov-12 3:39 
GeneralRe: My vote of 5 Pin
Arthanarieaswaran Shanmugaraj8-Nov-12 18:27
Arthanarieaswaran Shanmugaraj8-Nov-12 18:27 
GeneralMy vote of 5 Pin
ssushil971116-Aug-12 23:39
ssushil971116-Aug-12 23: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.