Click here to Skip to main content
15,887,214 members
Articles / Web Development / HTML
Tip/Trick

MVC Master Detail Example with Partial View and Modal Dialog

Rate me:
Please Sign up or sign in to vote.
4.91/5 (12 votes)
18 Nov 2015CPOL2 min read 46.9K   2.8K   23   4
MVC Master Detail example with partial view and modal dialog

Introduction

In this sample, I'm going to show how to develop an application for editing master-detail entities in a single page. In this sample, I have utilized JQuery Ajax feature as well as JQuery.UI dialog extension.

Using the Code

Home Page

Home Index view has been made of three DIVs: logo, navigation and contentFrame like this:

HTML
<table>
    <tr>
        <td colspan="2">
            <div id="logo"> </div>
        </td>
    </tr>
    <tr style="height:600px">
        <td id="navigation" style="width:200px; ">
            <div>
                @{Html.RenderAction("Index", "Orders");}
            </div>
        </td>
        <td>
            <div id="contentFrame">
            </div>
        </td>
    </tr>
</table>

I have selected Orders as master entity and have implemented it as a PartialView. This entity is loaded inside navigation DIV by using this code:

ASP.NET
@{Html.RenderAction("Index", "Orders");}

On controller side, we need to change Index controller action to return PartialView instead of View. Also, I have added an integer parameter to get page number from view:

C#
public ActionResult Index(int? page)
{
    var orders = db.Orders.Include(o => o.Customer).OrderByDescending(o => o.OrderDate);
    int pageNumber = page ?? 1;
    int pageSize = 3;
    return PartialView(orders.ToPagedList(pageNumber,pageSize));

Master Entity

As I mentioned, I have selected Order as master entity. To make this entity loaded as a PartialView inside contentFrame DIV, we need to change Create, Edit and Delete buttons. For Create button, first we set its id attribute to btnCreateNew to find this object through JavaScript:

JavaScript
@Html.ActionLink("Create New", "Create",new {}, new {id="btnCreateNew" })

Then, we wire up its click event to load Create View inside contentFrame:

JavaScript
$('#btnCreateNew').click(function (e) {
    e.preventDefault();
    $('#contentFrame').mask("waiting ...");
    $('#contentFrame').load(this.href, function (response, status, xhr) {
        $('#contentFrame').unmask("waiting ...");
    });
});

Because it might take time to load PartialView, it’s recommended to show a loadmask inside contentFrame when it is loading data:

JavaScript
$('#contentFrame').mask("waiting ...");

After loading PartailView was finished, loadmask should disappear with this function call:

JavaScript
$('#contentFrame').unmask("waiting ...");

We need to include these JS and CSS files in web application:

HTML
~/Scripts/jquery.mask.js
~/Content/jquery.loadmask.css

You may put these two files inside BundleConfig.cs as I did. Also, we should put this image file in application:

~/images/loading.gif

For more information about JQuery loadmask, please refer to this web address:

Because there is more than one Edit button, we set its btnName attribute to btnEdit to find these objects through JavaScript:

C#
@Html.ActionLink("Edit", "Edit", 
new { id = item.OrderID }, new { btnName = "btnEdit" }) 

Then, we wire up its click event to load Edit View inside contentFrame:

JavaScript
$('a[btnName=btnEdit]').click(function (e) {
    e.preventDefault();
    $('#contentFrame').mask("waiting ...");
    $('#contentFrame').load(this.href, function (response, status, xhr) {
        $('#contentFrame').unmask("waiting ...");
    });
});

Delete button is the same as Edit button.

Detail Entity

I have selected Order Items as detail entity. We show detail entity only in master entity's Edit View. So we need to load OrderItems Index view as a PartialView inside Order's Edit View and pass OrderID as a parameter to its action:

HTML
<div id="detailFrame">
    @{Html.RenderAction("Index", "OrderItems",new {OrderID = Model.OrderID});}
</div>

We need to get OrderID as a parameter in Index action of the OrderItems and filter Order Items to return only order items for this order:

C#
public ActionResult Index(int OrderID,int? page)
{
    ViewBag.OrderID = OrderID;
    var orderItems = db.OrderItems.Include(oi => oi.Order).Include
    (oi => oi.Product).OrderByDescending(oi => oi.OrderItemID);
    int pageSize = 3;
    int pageNumber = page ?? 1;
    return PartialView(orderItems.Where(oi => oi.OrderID == OrderID).ToPagedList(pageNumber,pageSize));
}

Create and Edit Detail Entity

For creating and editing detail entity, we need to open modal dialog that I already have developed an example about. For more information about creating and editing entity through modal dialog and adding other functionalities such as filtering and sorting, please take a look at my previous article titled: "MVC pagination, filtering and sorting inside partial view with edit in modal dialog" at this URL:

License

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


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks for this work Pin
arshadch1238-May-20 11:17
arshadch1238-May-20 11:17 
QuestionMVC Master Detail example question Pin
Minaz Hirji6-Jun-17 5:17
Minaz Hirji6-Jun-17 5:17 
SuggestionReal World Pin
Member 125940922-Sep-16 3:36
Member 125940922-Sep-16 3:36 
GeneralMy vote of 4 Pin
Santhakumar M18-Nov-15 7:53
professionalSanthakumar M18-Nov-15 7:53 

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.