asp.net mvc 在View中获取Url参数的值
2012-04-19 13:53
344 查看
如果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 mvc 在View中获取Url参数的值
- asp.net mvc 在View中获取Url参数的值
- asp.net mvc 如何在View中获取Url参数的值
- asp.net mvc 在View中获取Url参数的值
- asp.net mvc 在View中获取Url参数的值
- dhl:asp.net mvc 在View中获取Url参数的值
- asp.net mvc 在View中获取Url参数的值
- asp.net mvc 在View中获取Url参数的值
- asp.net mvc 在View中获取Url参数的值
- 在ASP.NET MVC 中获取当前URL、controller、action 、参数
- ASP.NET MVC中获取URL地址参数的两种写法
- ASP.NET MVC中获取URL地址参数的两种写法
- ASP.NET MVC中获取URL地址参数的两种写法
- ASP.NET MVC获取URL地址
- 在ASP.NET MVC 中获取当前URL、controller、action
- 如何在ASP.NET MVC 中获取当前URL、controller、action
- asp.net mvc学习笔记1(razor,html,url辅助器,ViewData,ViewBag)
- asp.net 正则获取url参数
- 在ASP.NET MVC 中获取当前URL、controller、action
- ASP.net获取当前url属性(文件名、参数、域名 等)的方法