2016-11-30 78 views
1

我的代碼:如何以URL格式將GET參數從一個cshtml頁面轉移到另一個頁面?

start.cshtml:

<a href="Page2.cshtml?parameter=one">link</a> 

Page2.cshtml:

Request.Params["parameter"] 

的get參數似乎沒有得到從開始頁到第2頁轉移,因爲當我嘗試在Page2上顯示「參數」時,我得到的錯誤是空的。

我該如何解決這個問題?

+0

這是MVC?一般在mvc中,你使用一個動作。 –

+1

我的教授說我應該通過我自己的URL生成鏈接:Page2.cshtml?parameter = one – noclue123

回答

0

您創建一個鏈接:

<%= Html.ActionLink("link", "Action", "Controller", new { parameter="one" }) %> 

MVC的核心

<a asp-controller="Controller" asp-action="Action" asp-route-parameter="one" >link</a> 

然後,你需要有一個動作接收該參數:

public async Task<IActionResult> Action(string parameter) 
{ 
    ViewBag.parameter = parameter; 
    return View() 
} 

而且在你看來,你只是顯示此參數:

<label>Parameter:</label> @ViewBag.parameter 
0

如果你想實現無輔助類故意的,那麼下面是

syntax: 
<a href="ControllerName/Action(or)PageName?parameter=one">link</a> 

Example: 
<a href="yourcontrollername/Page2?parameter=one">link</a> 

你注意工作代碼:只需要給動作/視圖名稱單獨即第2頁,不應指定爲page.cshtml 。

希望這會有所幫助,請讓我知道你的想法或反饋

感謝 KARTHIK

+0

在哪裏可以找到我的控制器名?爲什麼不用「page2.cshtml」來指定呢?我的教授說我應該在URL中自己生成鏈接:Page2.cshtml?parameter = one – noclue123

相關問題