asp.net-mvc 如何从 URL (ASP.NET MVC) 获取视图中的当前路由 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/979532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the current route ID within a View from the URL (ASP.NET MVC)
提问by JMS
Within the view that is returned from a URL such as /Controller/Action/1 (assuming the default route of controller/action/id), how can I get access to the ID from within the View?
在从 /Controller/Action/1 等 URL 返回的视图中(假设默认路由为 controller/action/id),如何从视图中访问 ID?
I don't want to have to add it to the ViewData dictionary at the action level when handling the request.
我不想在处理请求时将它添加到操作级别的 ViewData 字典中。
回答by Francisco
I think this is what you're looking for:
我认为这就是你要找的:
<%=Url.RequestContext.RouteData.Values["id"]%>
回答by CoderDennis
ViewData is exactly the right way of doing this.
ViewData 正是这样做的正确方法。
Your other option would be to pass a model that contains ID to the view.
您的另一个选择是将包含 ID 的模型传递给视图。
Edit:Without knowing exactly what you're tying to do, it's tough to give more specific advise. Why do you need an ID, but not any other model data? Is your controller really only sending the Id field to the view? It's hard to imagine what the scenario is.
编辑:在不确切知道您要做什么的情况下,很难给出更具体的建议。为什么您需要 ID,而不需要任何其他模型数据?您的控制器真的只将 Id 字段发送到视图吗?很难想象这是什么场景。
If the ID value is truly the only model information that is being passed to your view, then you could use the ID itself as the model. Then the return value of your action method would be View(id)and you wouldn't need to use ViewData.
如果 ID 值确实是传递给您的视图的唯一模型信息,那么您可以使用 ID 本身作为模型。然后您的操作方法的返回值将是View(id)您不需要使用 ViewData。
回答by Wyatt Barnett
Adding it to the viewdata is the right thing to do. As for how to add it, you could always add a custom ActionFilterwhich grabs it from the route dictionary and pushes it into the viewdata.
将它添加到 viewdata 是正确的做法。至于如何添加它,您可以随时添加一个自定义ActionFilter,从路由字典中获取它并将其推送到视图数据中。
回答by A.M. Patel
I think you can direct use in url action Url.RequestContext.RouteData.Values["id"]
我认为你可以直接在 url 动作中使用 Url.RequestContext.RouteData.Values["id"]
回答by pimbrouwers
Just my two cents, but I always use view models for passing any data to my views. Even if it's as simple as needing an intlike the id.
只是我的两分钱,但我总是使用视图模型将任何数据传递给我的视图。即使它就像需要一个int像 id一样简单。
Doing this makes accessing this value trivial, as MVC does all the work for you.
这样做会使访问此值变得微不足道,因为 MVC 为您完成了所有工作。
For what it's worth I typically namemy view models as such:
对于它的价值,我通常将我的视图模型命名为:
{Controller}{ViewName}ViewModel
{Controller}{ViewName}ViewModel
This helps keeps things organized at scale.
这有助于使事情按比例组织。
An example:
一个例子:
// ~/ViewModels/HomeEditViewModel.cs
public class HomeEditViewModel
{
public int Id { get; set; }
}
// ~/Controllers/HomeController.cs
public IActionResult Edit(int id)
{
return View(new HomeEditViewModel() { Id = id });
}
// ~/Views/Home/Edit.cshtml
@model HomeEditViewModel
<h1>Id: @Model.Id</h1>
回答by RAM
We can pass idas Route Dataor QueryString, so to support both of themi recommend this way:
我们可以通过id为Route Data或QueryString,所以支持他们的我推荐这种方式:
var routeValues=Url.RequestContext.RouteData.Values;
var paramName = "id";
var id = routeValues.ContainsKey(paramName)?
routeValues[paramName]:
Request.QueryString[paramName];
回答by Poyi Hong
If you are using .NET Core
如果您使用的是.NET Core
Use this code
使用此代码
Url.ActionContext.RouteData.Values["id"]
回答by TroySteven
Simple Answer In Razor:
Razor 中的简单答案:
@Url.RequestContext.RouteData.Values["id"]
回答by Abou-Emish
i just used <%= model.id %>and it works
我刚用过<%= model.id %>,效果很好

