博客
关于我
ASP.NET MVC5---通过QueryString传值
阅读量:415 次
发布时间:2019-03-06

本文共 1115 字,大约阅读时间需要 3 分钟。

1.首先我们来看看普通的传值是啥样的。如图所示,我们普通的传值是这样的

1    public ActionResult Edit(int?id) 2         { 3              4             if (id == null) 5             { 6                return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 7             } 8             9 10             Movie movie = db.Movies.Find(id);11             if (movie == null)12             {13                 return HttpNotFound();14             }15             return View(movie);16         }

在列表页点击的时候,是这样的效果:

 

再来看看我们的QueryString传值的效果吧:

 

现在来说说怎么实现的吧,首先我们在列表页面(Index)加一个链接(Click Me),具体代码如下:

 @Html.ActionLink("Click Me", "Edit", "Movies", new{word=item.ID}, new { @class="myClass"})

注意:这里面的匿名对象word必须是word不能是其他的名称,已经亲测。

然后,我们在控制器里面找到Edit方法,在里面获取Index页面传过来的值,这里是word(对应主键值ID)

1    public ActionResult Edit() 2         { 3              4             ViewBag.myID = Convert.ToInt32( Request.QueryString["word"]); 5  6             Movie movie = db.Movies.Find(ViewBag.myID); 7             if (movie == null) 8             { 9                 return HttpNotFound();10             }11             return View(movie);12         }

最后运行项目就是上面的那样的的效果了。编辑功能也没有任何影响。

转载地址:http://sxxkz.baihongyu.com/

你可能感兴趣的文章
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>