Click here to Skip to main content
11,720,545 members (74,661 online)
Click here to Skip to main content

Basics of ASP.NET MVC3: Part-II

, 23 Mar 2012 CPOL 38.6K 2.3K 50
Rate this:
Please Sign up or sign in to vote.
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.

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:

  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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
Questionany thing to share about window phone game development Pin
imransaharp17-Mar-14 21:39
memberimransaharp17-Mar-14 21:39 
AnswerRe: any thing to share about window phone game development Pin
Arthanarieaswaran17-Mar-14 22:37
memberArthanarieaswaran17-Mar-14 22:37 
GeneralMy vote of 5 Pin
Ashish Tripathi5-Nov-12 3:39
memberAshish Tripathi5-Nov-12 3:39 
GeneralRe: My vote of 5 Pin
Arthanarieaswaran8-Nov-12 18:27
memberArthanarieaswaran8-Nov-12 18:27 
GeneralMy vote of 5 Pin
ssushil971116-Aug-12 23:39
memberssushil971116-Aug-12 23:39 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150901.1 | Last Updated 23 Mar 2012
Article Copyright 2012 by Arthanarieaswaran
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid