This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12993|Reply: 0

[ASP.NET] asp.net mvc gets the value of the Url parameter in the View

[Copy link]
Posted on 11/17/2015 6:47:35 PM | | |

If the url is /home/index?id=3, direct Request is OK.
But if the route is set to: {controller}/{action}/{id}
The url is /home/index/3   
How do I get the value of the parameter id in the page view?

After checking the information, many people use Action to obtain the parameter value and use Viewdata to transfer it to View
For example
phonelist in Controlers  
public ActionResult phonelist(int id)  
  {  
  ViewData["id"] = id;   
  return View();  
  }  

Actually, it's not necessary, just get it like this in the view:
<%=Html.ViewContext.RouteData.Values["id"]%>
Even if there is no ID parameter, the error will not be reported.

Similarly: <%=Request.RequestContext.RouteData.Values["id"] %>sum
<%=Html.ViewContext.RouteData.Route.GetRouteData(Html.ViewContext.HttpContext). Values["id"]%>
can also be picked up.


Note: RouteData cannot be directly accessed in the user control, RouteData is a property in the Page object.
So you need to use this. Page.RouteData to get the parameters
Use this. Page.RouteData.Values["id"]to get the value of the parameter




Previous:Share a URL for Apple to check whether the iOS flashing firmware is turned off
Next:jquery listens for carriage return events
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com