Get Selected Row from ASP.NET MVC 3 WebGrid

Abstract: The following article demonstrates how to get the selected row from the ASP.NET MVC 3 WebGrid and what to do to display data.

Every website has to display data and every website has a Grid control. In ASP.NET MVC 3 there’s the WebGrid, which is part of the Microsoft Web Helpers library. This can be downloaded through NuGet (formerly NuPack). NuGet is a free open source package manager that makes it easy for you to find, install, and use .NET libraries in your projects. One piece of functionality that is critical is reacting when the user selects an item in the WebGrid. This article will focus on finding out which row was selected, but also how to find out more about the data that is selected.

Before moving on, you need to download ASP.NET MVC 3. Click here to download and install them using the Microsoft Web Platform Installer.

Open studio 2010 and create a new ASP.NET MVC 3 Web Application (Razor) project. To focus on the answer, I’ve got a simple model as seen below.

Using the WebGrid, it’s easy to display this data to the user.

The first column is the key to making this work. @item.GetSelectLink outputs a HTML anchor tag with the row selected. This is passed as aQueryString, and the name of the QueryString is set by the selectionFieldName property set on the grid.

To find out what row is selected is just as easy. The WebGrid has a property called SelectedRow. This sets a reference to a GridViewRow object that represents the selected row in the control. When you combine this with the HasSelection property, you can get the selected row like this.

I’ve created a partial view called _Person.cshtml. The file begins with an underscore (_) because I don’t want this file called directly from the web. The second parameter is the data being passed into the partial view. The data in this instance is the selected row.

The partial view has then got access to all the data in the selected row. Nice and easy. Thanks Microsoft!!

The entire source code of this article can be downloaded over here

[webgrid] – selecterow - (Get Selected Row from ASP.NET MVC 3 WebGrid)的更多相关文章

  1. ASP.NET MVC 的 WebGrid 的 6 个重要技巧 【已翻译100%】

    ASP.NET MVC 中 WebGrid 的 6 个重要技巧 https://www.oschina.net/translate/webgrid-in-asp-net-mvc-important-t ...

  2. The Three Models of ASP.NET MVC Apps

    12 June 2012  by Dino Esposito by Dino Esposito   We've inherited from the original MVC pattern a ra ...

  3. asp.net mvc 学习资料

    ASP.NET MVC 的 WebGrid 的 6 个重要技巧 http://www.oschina.net/translate/webgrid-in-asp-net-mvc-important-ti ...

  4. 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)

    在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...

  5. ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】

    ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebG ...

  6. ASP.NET MVC搭建项目后台UI框架—6、客户管理(添加、修改、查询、分页)

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  7. ASP.NET MVC搭建项目后台UI框架—7、统计报表

    ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NET M ...

  8. ASP.NET MVC搭建项目后台UI框架—8、将View中选择的数据行中的部分数据传入到Controller中

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  9. jQuery DataTables and ASP.NET MVC Integration

    part 1 : http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Pa ...

随机推荐

  1. PV、UV、IP之间的区别与联系

    先看图: 1.什么是PV值 PV(page view)即页面浏览量或点击量,是衡量一个网站或网页用户访问量.具体的说,PV值就是所有访问者在24小时(0点到24点)内看了某个网站多少个页面或某个网页多 ...

  2. list 集合

    1.Model public class ROLE_FUNCTION { //角色集合 public List< ROLE> ROLES { get; set; } //角色权限集合 pu ...

  3. IOS-App installation failed原因及解决方法

    在用真机调试的时候,每次运行app都会弹出这个框,点击ok后,再次运行就没问题了.刚开始觉得没什么,无非就是多点几次,到后来,觉得这也很浪费时间,严重影响开发效率,在网上搜索了一遍后,决定将解决方案写 ...

  4. the comment lines of the blast tabular format

    进行Blast比对,用参数-m 6 可以以列表的方式输出结果,结果中从左到右每一列的意义分别是: [00] Query id [01] Subject id [02] % identity [03] ...

  5. 浅谈malloc()与free()

    malloc()与free() l  函数原型 malloc函数的函数原型为:void* malloc(unsigned int size),它根据参数指定的尺寸来分配内存块,并且返回一个void型指 ...

  6. python-汉诺塔递归实现

    摘录自廖雪峰老师教程下的评论,个人备忘,脑细胞已死光 def move(from,to): #将盘子从from移动到to,动画效果需要脑补 print(from,'->',to) def han ...

  7. oracle sqlplus 连接不正常

    场景描述:在开始--运行--输入SQLPLUS 登陆不了报警:“WINDOWS找不到文件‘SQLPLUS’. 原因分析:一般出现这种情况可能的原因: 1.文件名有问题 2.路径有问题 3.安装有问题 ...

  8. SPOJ GSS4 Can you answer these queries IV

    Time Limit: 500MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

  9. android.content.SharedPreferences.edit()

    今天在实现一个保存用户设置到SharedPreferences时,出现了一个不能将数据保存到SharedPreferences中的情况.经过仔细的分析得出: android.content.Share ...

  10. mtd零星记录

    查看Flash分区情况: root@DD-WRT:~# cat /proc/mtd dev: size erasesize name mtd0: "RedBoot" mtd1: 0 ...