开发者

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

开发者 https://www.devze.com 2022-12-21 19:16 出处:网络
I have an URL like this /home/action/id开发者_JS百科 How can I access this id in view?This should work in your view:

I have an URL like this /home/action/id开发者_JS百科

How can I access this id in view?


This should work in your view:

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

(assuming the route parameter is named "id")


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

精彩评论

暂无评论...
验证码 换一张
取 消