2013-10-17 67 views
1

我的網址URL值是一樣的東西,獲得 「ID」 從MVC 4

localhost:19876/PatientVisitDetail/Create?PatientId=1

我要檢索的URL PatientId並將其傳遞的請求。

我試過,

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' 

我又一次試圖,

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

編輯:

基於傑森的評論下面,我想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) 

所以,我必須在此情況下,沿着patientId通過。

+0

由於'PatientId'是查詢字符串參數,請看看這個:http://stackoverflow.com/questions/2888105/how-to-access-querystring-in-asp-net-mvc-view –

+0

@JasonEvans :感謝您的鏈接。有效。但是,我也看到一個警告,「不訪問視圖中的url參數?」。在我的情況下,如何避免這種情況? –

回答

7

您有效規避MVC整點這裏。有一個行動,接受PatientId

public ActionResult Create(int patientId) 
{ 
    return View(patientId); 
} 

然後在您的視圖中使用該值,例如,

@model int 

@Html.ActionLink("Back", "LastAction", "LastController", new { patientId = @Model }) 

這是MVC方式。

+0

乾淨的解決方案。謝謝 –

+0

完美!整潔乾淨的解決方案,我在這裏找到了。謝謝! @詹姆士 – waghekapil

2

從你的控制器,你可以把PatientIdViewBag並從您的視圖訪問

public ActionResult Create() 
{ 

    ViewBag.PatientId = 1; 
    return View(); 
} 

查看

Html.ActionLink("Back", "Index", new { PatiendId = ViewBag.PatientId }) 
+0

也可以。 +1。謝謝。 –