zoukankan      html  css  js  c++  java
  • asp.net mvc 如何在View中获取Url参数的值

    如果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"]%>

    也可以取到。

    有了这层认识,你会发现,MVC的大部分你想要的东西都可以从ViewContext中拿到

    引用路径:http://www.cnblogs.com/leo_wl/p/3504050.html

  • 相关阅读:
    20190817-T1-LOJ6322「雅礼国庆 2017 Day6」Star Way To Heaven
    20190817-涪
    20190816-周期
    考试总结 模拟95
    考试总结 模拟94
    考试总结 模拟93
    考试总结 模拟92
    考试总结 模拟91
    考试总结 模拟90
    考试总结 模拟89
  • 原文地址:https://www.cnblogs.com/baiyunchen/p/4626443.html
Copyright © 2011-2022 走看看