从MVC 4中的URL获取"Id"值

now*_*ed. 1 c# url routing .net-4.0 asp.net-mvc-4

我的网址是这样的,

localhost:19876/PatientVisitDetail/Create?PatientId=1

我必须PatientId从URL中检索并将其传递给请求.

我试过了,

    Url.RequestContext.Values["PatientId"] => System.Web.Routing.RequestContext does not contain a definition for 'Values' and
no extension method 'Values' accepting a first argument of type 'System.Web.Routing.RequestContext'
Run Code Online (Sandbox Code Playgroud)

我再试一次,

RouteData.Values["PatientId"]  => an object reference is required for the non static field, method
or property 'System.Web.Routing.RouteData.Values.get'
Run Code Online (Sandbox Code Playgroud)

编辑:

根据杰森在下面的评论,我试过Request["SomeParameter"]并且它有效.但是,还有一个警告要避免这种情况.

任何想法如何避免我的情况?

我的情景:

Create我的控制器中有一种用于创建新患者的动作方法.

但是,我需要回到最后一页,

如果我给出类似的东西,

 @Html.ActionLink("Back to List", "Index") 


=> this wont work because my controller action method has the following signature,

public ActionResult Index(int patientId = 0)
Run Code Online (Sandbox Code Playgroud)

所以,我必须patientId在这种情况下传递.

Jam*_*mes 7

你在这里有效地规避了MVC的重点.有一个接受的行动PatientId

public ActionResult Create(int patientId)
{
    return View(patientId);
}
Run Code Online (Sandbox Code Playgroud)

然后在您的视图中使用该值,例如

@model int

@Html.ActionLink("Back", "LastAction", "LastController", new { patientId = @Model })
Run Code Online (Sandbox Code Playgroud)

这是MVC的方式.