How do I access a route parameter in my ASP.NET MVC view?

前端 未结 2 589
后悔当初
后悔当初 2020-12-29 05:09

I have an URL like this /home/action/id

How can I access this id in view?

相关标签:
2条回答
  • 2020-12-29 05:09

    This should work in your view:

    <%= this.ViewContext.RouteData.Values["id"] %>
    

    (assuming the route parameter is named "id")

    0 讨论(0)
  • 2020-12-29 05:36

    you can pass it in through viewData;

    In your Controller:

    public ActionResult Index(string id)
    {
        ViewData["Name"] = Server.UrlEncode(id);
        return View();
    }
    

    In your View:

    <h1><%= ViewData["Name"] %></h1>
    
    0 讨论(0)
提交回复
热议问题