簡體   English   中英

如何調用 controller Action 從模態 partialView 傳遞 id 參數?

[英]How to call a controller Action passing id parameters from a modal partialView?

我有一個 ASP.Net Core MVC5 項目,其視圖包含部分視圖。 在其中,我展示了一個從上下文中的記錄加載數據的 Bootstrap 模式。 我需要將此記錄的標識符作為<a>標記中的參數傳遞,該標記重定向到同一 Controller 的另一個操作。

我試圖像這樣安裝它:

<a asp-controller="Controller" asp-action="Action" asp-route="@ViewBag.Value" class="btn btn-default">
  <i class="fas fa-file-invoice-dollar"></i> 
  Button
</a>

但是它不起作用,它不識別 ViewBag。

我也像這樣嘗試使用 Onclick 腳本,但id參數到達時為 null。

$('#idA').attr('action', '/Controller/Action/'@ViewBag.id);

有沒有人做過這樣的事情或者可以幫助我?

您可以使用 asp-route-id

<a asp-controller="Controller" asp-action="Action" asp-route="@ViewBag.Value" asp-route-id="@ViewBag.id" class="btn btn-default">
  <i class="fas fa-file-invoice-dollar"></i> 
  Button
</a>

您可以使用 javascript 或 jquery 獲取值,使用 ajax 發布到 controller,如下所示:

https://stackoverflow.com/a/10653696/7687161

你不應該結合 asp-route="xx" 和 asp-controller="xx" 試試這個:

<a asp-controller="Controller" asp-action="Action" asp-route-Id="@ViewBag.Id" class="btn btn-default">
    <i class="fas fa-file-invoice-dollar"></i>
    Button
</a>

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM