public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(entities.Customers.ToList());
}
}
@model IEnumerable<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 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(null, null, format: @<text>@Html.ActionLink("Select", null, null, new { @onclick = "return GetSelectedRow(this);" })</text>)
))
<script type="text/javascript">
function GetSelectedRow(link) {
var row = link.parentNode.parentNode;
var message = "Selected Row:";
message += "\n\nCustomer Id: " + row.getElementsByTagName("TD")[0].innerHTML;
message += "\nContact Name: " + row.getElementsByTagName("TD")[1].innerHTML;
message += "\nCity: " + row.getElementsByTagName("TD")[2].innerHTML;
message += "\nCountry: " + row.getElementsByTagName("TD")[3].innerHTML;
alert(message);
return false;
}
</script>
</body>
</html>