In this article I will explain with an example, how to select a WebGrid row 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.
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 Select Row: Get WebGrid selected row value in ASP.Net MVC
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
This Action method handles the call when a WebGrid row is selected.
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());
    public ActionResult Details(string customerId)
        NorthwindEntities entities = new NorthwindEntities();
        return PartialView("Details", entities.Customers.Find(customerId));
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 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 CustomerId value 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>
    <meta name="viewport" content="width=device-width"/>
    <style type="text/css">
            font-family: Arial;
            font-size: 10pt;
            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;
        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>)
    @if (webGrid.HasSelection)
        string customerId = (webGrid.SelectedRow.Value asCustomer).CustomerID;
        Html.RenderAction("Details", new { customerId = customerId });
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_SelectedRow_MVC.Customer
<table cellpadding="0" cellspacing="0" border="0">
        <td valign="top" style="width:65px"><b>@Html.DisplayNameFor(model => model.Address):</b></td>
            @Html.DisplayFor(model => model.Address)
            @Html.DisplayFor(model => model.City),
            @Html.DisplayFor(model => model.PostalCode)
            @Html.DisplayFor(model => model.Country)
MVC WebGrid Select Row: Get WebGrid selected row value in ASP.Net MVC