asp.net mvc 在View中获取Url参数的值
2016-05-31 07:50
537 查看
如果url是 /home/index?id=3 直接Request就ok。
但是如果路由设定为:{controller}/{action}/{id}
url是 /home/index/3
这时想在页面View中获取参数id的值,该怎么获取?
查了下资料好多人都利用Action获取到参数值后,用Viewdata传到View中
例如
Controlers中的phonelist这样定义
public ActionResult phonelist(int id)
{
ViewData["id"] = id;
return View();
}
其实,没有必要,只要在view中这样获取就可以:
<%=Html.ViewContext.RouteData.Values["id"]%>
就算没有id的参数也不会报错。
同样:<%=Request.RequestContext.RouteData.Values["id"] %>和
<%=Html.ViewContext.RouteData.Route.GetRouteData(Html.ViewContext.HttpContext).Values["id"]%>
也可以取到。
在用户控件中是无法直接访问到RouteData,RouteData是Page对象中的属性,
所以需要在用户控件中使用this.Page.RouteData来获取参数
使用this.Page.RouteData.Values["id"]来获取参数的值
但是如果路由设定为:{controller}/{action}/{id}
url是 /home/index/3
这时想在页面View中获取参数id的值,该怎么获取?
查了下资料好多人都利用Action获取到参数值后,用Viewdata传到View中
例如
Controlers中的phonelist这样定义
public ActionResult phonelist(int id)
{
ViewData["id"] = id;
return View();
}
其实,没有必要,只要在view中这样获取就可以:
<%=Html.ViewContext.RouteData.Values["id"]%>
就算没有id的参数也不会报错。
同样:<%=Request.RequestContext.RouteData.Values["id"] %>和
<%=Html.ViewContext.RouteData.Route.GetRouteData(Html.ViewContext.HttpContext).Values["id"]%>
也可以取到。
在用户控件中是无法直接访问到RouteData,RouteData是Page对象中的属性,
所以需要在用户控件中使用this.Page.RouteData来获取参数
使用this.Page.RouteData.Values["id"]来获取参数的值
相关文章推荐
- ASP.NET资料搜集
- vs项目属性中目标平台x86,x64,any cpu的区别
- win10下vs2015创建asp,net core项目并运行在ubuntu14.04下
- 【AspNet Core】Nuget代理网站
- Dotnetnuke安装
- .net mvc web api上传图片/文件并重命名
- ubuntu 下安装metasploit
- ASP.NET里的Session详细解释
- ASP.NET多次点击提交按钮以及Session超时和丢失过期问题
- 【译】Asp.net mvc 使用ITextSharp PDF to HTML (解决img标签问题)
- Java整合Sikuli和Asprise.OCR实现桌面自动化
- 优秀ASP.NET程序员修炼之路
- asp.net mvc 5 蛋疼的问题
- [ASP.NET MVC 小牛之路]16 - Model 验证
- Raspberry Pi3 ~ 安装 nfs Server
- ASP.NET Core 开发-中间件(StaticFiles)使用
- ASP.NET vNext 概述
- ASP.NET页面——视图状态
- 无法激活服务,因为它不支持 ASP.NET 兼容性
- 注册asp.net 4.0 到iis