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. Oracle-函数Decode进行多值判断

    decode函数比较表达式和搜索字,如果匹配,返回结果:如果不匹配,返回default值:如果未定义default值,则返回空值 Decode函数的语法结构如下: decode (expression ...

  2. jsoup-提示java.net.SocketTimeoutException:Read timed out

    使用Jsoup.connect(url).get()连接某网站时偶尔会出现 java.net.SocketTimeoutException:Read timed out异常. 原因是默认的Socket ...

  3. 【收藏】Java多线程/并发编程大合集

    (一).[Java并发编程]并发编程大合集-兰亭风雨    [Java并发编程]实现多线程的两种方法    [Java并发编程]线程的中断    [Java并发编程]正确挂起.恢复.终止线程    [ ...

  4. 数据库开发基础-SQl Server 基础

    SQL Server 基础 1.什么是SQL Server SQL:Structured Query Language  结构化查询语言 SQL Server是一个以客户/服务器(c/s)模式访问.使 ...

  5. Java多线程与并发库高级应用-工具类介绍

    java.util.concurrent.Lock 1.Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互 ...

  6. macOS 安装 wget

    适用于macOS Sierra Apple Store下载安装Xcode 安装Homebrew包管理,类似于Ubuntu下的apt-get: 终端下输入 ruby -e "$(curl -f ...

  7. app基本信息

    1.获取设备信息 NSLog(@"设备名称:%@",[[UIDevice currentDevice] systemName]); NSLog(@"版本号:%@" ...

  8. 再次深入探索datasource问题?

    datasource现在几乎每个web框架都会有集成,但是对于数据源的设计原理以及应用上,很少进行深入的研究:实际上数据源也是web框架的核心之一了解一下其内涵还是非常重要的. 数据源(Data So ...

  9. C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果)

    http://blog.csdn.net/jiangxinyu/article/details/6222322/ 转载自CSDN

  10. shutil模块

    shutil模块 提供了大量的文件的高级操作,特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作 常用方法 shutil.copyfile(src, dst) 复制文件内容(不包含元数据)从 ...