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.
Controller
The Controller consists of the following Action method.
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.
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
HTML Markup
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.
@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>
</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 HYPERLINK "https://www.aspsnippets.com/Categories/Partial-View.aspx" \t "_blank" 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
Downloads