2017-08-09 15 views
0

如何在我的視圖中獲取傳遞參數。 我在我的控制器C#mv5通過鏈接傳遞參數然後在視圖中獲取

public ActionResult report() 
     { 

      return View(); 
     } 

../Map/report/Inventory

的URL已經顯示了通數據,但我怎樣才能得到它通過使用

<td> 
<a href="@Url.Action("report", "Map" , new { id = "Inventory"})" target="_blank"> Inventory Reports</a> 
</td> 

參數jQuery或JavaScript

+0

使用它,我想你已經習慣'公衆的ActionResult報告(字符串ID)'(ID = URL參數)。當控制器返回視圖後,你期望什麼? –

+0

我只需要在JavaScript中獲得通過值。 – gray

+1

使用ViewBag/ViewData&放入這樣的JS:'var id ='@ ViewBag.Id''(或使用viewmodel)。 –

回答

2

從您的路徑時使用的參數,你可以從你的控制器通過視圖模型或只是viewbag傳回的網頁,如果這所有你必須:

public ActionResult report(string id) 
{ 
    ViewBag.id = id; 
    return View(); 
} 

在你看來,你可以將值寫入隱藏輸入

@Html.Hidden("id", ViewBag.id) 

然後,在JavaScript可以讀取id的值,併爲你喜歡

var id = document.getElementsByName("id")[0].value; 
1

您還可以使用lastIndexOf()函數來定位URL中/字符的最後出現位置,然後使用substr()函數返回從該位置串開始:

var value =this.href.substring(this.href.lastIndexOf('/') + 1); 

alert(window.location.href.substring(window.location.href.lastIndexOf('/') + 1))