In this article I will explain with an example, how to build a Master Detail application using WebGrid in ASP.Net MVC Razor.
The records from the database i.e. Master data will be displayed in WebGrid and the details of the WebGrid row will be displayed in Partial View inside jQuery Modal Popup.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
MVC WebGrid Master Detail example: Display details of WebGrid Row inside Popup using jQuery in ASP.Net MVC
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the Northwind Database.
Inside the Index Action method, the Customer records are fetched using Entity Framework and returned to the View.
 
Action method for handling jQuery POST operation
This Action method handles the call made from the jQuery POST function from the View.
The value of the customerId parameter is used to fetch the Customer record using Entity Framework which is then used to populate the Details Partial View.
Finally the Partial View is returned from the Controller.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        NorthwindEntities entities = new NorthwindEntities();
        return View(entities.Customers.ToList());
    }
 
    [HttpPost]
    public ActionResult Details(string customerId)
    {
        NorthwindEntities entities = new NorthwindEntities();
        return PartialView("Details", entities.Customers.Find(customerId));
    }
}
 
 
View
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that the Model will be available as a Collection.
The WebGrid is initialized with the Model i.e. IEnumerable collection of Customer Entity class objects as source.
The WebGrid is created using the GetHtml method with the following parameters.
HtmlAttributes – It is used to set the HTML attributes to the HTML Table generated by WebGrid such as ID, Name, Class, etc.
Columns – It is used to specify the columns to be displayed in WebGrid and also allows to set specific Header Text for the columns.
The last column of the WebGrid consists of an HTML Anchor Link. The HTML Anchor Link has been assigned a jQuery Click event handler.
When the HTML Anchor Link is clicked, a jQuery AJAX Call is made to the Details Action method of the Controller and the Details Partial View is fetched as HTML which is finally displayed using jQuery Modal Dialog Popup window.
Note: For more details on how to use jQuery AJAX for calling Controller’s Action method in ASP.Net MVC, please refer my article ASP.Net MVC: jQuery AJAX and JSON Example.
 
@model IEnumerable<WebGrid_ModalPopup_MVC.Customer>
 
@{
    Layout = null;
    WebGrid webGrid = new WebGrid(source: Model, canSort: false);
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
 
        .Grid
        {
            border: 1px solid #ccc;
            border-collapse: collapse;
        }
 
        .Grid th
        {
            background-color: #F7F7F7;
            font-weight: bold;
        }
 
        .Grid th, .Grid td
        {
            padding: 5px;
            border: 1px solid #ccc;
        }
 
        .Grid, .Grid table td
        {
            border: 0px solid #ccc;
        }
 
        .Grid th a, .Grid th a:visited
        {
            color: #333;
        }
    </style>
</head>
<body>
    @webGrid.GetHtml(
        htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
        columns: webGrid.Columns(
                 webGrid.Column("CustomerID", "Customer Id"),
                 webGrid.Column("ContactName", "Customer Name"),
                 webGrid.Column("City", "City"),
                 webGrid.Column("Country", "Country"),
                 webGrid.Column("Details", "", format: @<text><a href="javascript:;" class="details">View</a></text>)
      ))
    <div id="dialog" style="display: none">
    </div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js"></script>
    <link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css"/>
    <script type="text/javascript">
        $(function () {
            $("#dialog").dialog({
                autoOpen: false,
                modal: true,
                title: "View Details"
            });
            $("#WebGrid .details").click(function () {
                var customerId = $(this).closest("tr").find("td").eq(0).html();
                $.ajax({
                    type: "POST",
                    url: "/Home/Details",
                    data: '{customerId: "' + customerId + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "html",
                    success: function (response) {
                        $('#dialog').html(response);
                        $('#dialog').dialog('open');
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
           });
        });
    </script>
</body>
</html>
 
 
Partial View
In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option to create a View for the Controller.
The Name of the View is set to Details, the Template option is set to Empty, the Model class is set to Customer Entity (the one we have generated using Entity Framework), the Data context class is set to NorthwindEntities and finally the Create as a partial view option needs to be checked.
Inside the Partial View, in the very first line the Customer Entity is declared as Model for the Partial View. The details of the Customer is displayed using the Html.DisplayFor helper method.
@model WebGrid_ModalPopup_MVC.Customer
 
<table cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td valign="top"><b>@Html.DisplayNameFor(model => model.Address):</b></td>
        <td>
            @Html.DisplayFor(model => model.Address)
            <br/>
            @Html.DisplayFor(model => model.City),
            @Html.DisplayFor(model => model.PostalCode)
            <br/>
            @Html.DisplayFor(model => model.Country)
        </td>
    </tr>
</table>
 
 
Screenshot
MVC WebGrid Master Detail example: Display details of WebGrid Row inside Popup using jQuery in ASP.Net MVC
 
 
Downloads