Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Basics of ASP.NET MVC3: Part-II

0.00/5 (No votes)
21 Mar 2012 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here