MVC WebGrid Select Row: Get WebGrid selected row value in ASP.Net MVC
This article will also explain, how to get the value of the WebGrid selected row and pass the value of the WebGrid selected row to Partial View in ASP.Net MVC Razor.
This article will also explain, how to get the value of the WebGrid selected row and pass the value of the WebGrid selected row to Partial View in ASP.Net MVC Razor.
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 GET operation of Partial 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());
}
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.
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 a Select Link, generated using GetSelectLink Helper method.
The Select Link is used to select a WebGrid row. When a row in WebGrid is selected, a check is performed to make sure that the WebGrid has a selection and the CustomerIdvalue from the selected row of WebGrid is passed to the Partial View which is rendered using the Html.RenderAction Helper method.
@model IEnumerable<WebGrid_SelectedRow_MVC.Customer>
@using WebGrid_SelectedRow_MVC;
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model, canSort: false, rowsPerPage: 5);
}
<!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 a, .Grid a:visited
{
color: blue;
}
</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("Select", null, format: @<text>@item.GetSelectLink("Select")</text>)
))
<br/>
@if (webGrid.HasSelection)
{
string customerId = (webGrid.SelectedRow.Value asCustomer).CustomerID;
Html.RenderAction("Details", new { customerId = customerId });
}
</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.DisplayForhelper method.
@model WebGrid_SelectedRow_MVC.Customer
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" style="width:65px"><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>
Your work is so good I like Your articles writing your writing is so clear I liked it you are a great writer. I appreciate your work. ncert solutions class
ReplyDelete