簡體   English   中英

通過組合的動作鏈接和ASP.Net MVC 5中的輸入將用戶輸入的輸入從View傳遞到Action Controller?

[英]Passing a user-typed input from View to Action Controller via combined action link and input in ASP.Net MVC 5?

這是一個ASP.Net MVC 5項目。

我已閱讀以下有用的文章:

ASP.Net MVC如何將數據從視圖傳遞到控制器

解釋了如何在ASP.Net MVC中將數據從View傳遞到Controller 帖子中的主要思想是

  1. 使用View中的matching-model form-post(輸入)和Action Controller name-convention-parameters,或
  2. 創建一個動作鏈接,其中我們有一個匿名對象,該對象具有名稱約定參數,因為route valuesAction Controller的參數匹配。

現在,我在這里有一個略有不同的情況...,並希望獲得一些解決方法的想法。

與上面的方法(2)相似,我已經有一個帶有匿名對象的動作鏈接,該對象具有使用剃刀動態生成的名稱會議參數,如下所示:

@Html.ActionLink("Detail", item.DetailsName, item.ControllerName,
  new {
    pid = item.Pid, eid = item.Eid, mid = item.Mid, cid = item.Cid,
    manNo = item.TypeItem == "Create" ? item.ManCreateNo : item.ManEditNo,
    caneNo = item.TypeItem == "Create" ? item.CaneCreateNo : item.CaneEditNo,
    command = userCanAccept ? "acc" : "pass", from = ViewBag.From
  }, null)

並且由ActionLink調用的(可能的一種)對應的Action Controller如下所示:

public ActionResult DetailsChange(int pid, int eid, int mid, int cid, int manNo = 1, int caneNo = 1, string command = null, string from = null, string comment = null)

現在,你可以看到,在Action Link的參數的ViewAction Controller “中的S參數Controller完美匹配, 除了一: comment

所有其他參數都可以在呈現時由“按剃刀View呈現,但是comment應該是用戶輸入的輸入(文本)。

現在,當按下“ Action Link時,如何將commentView傳遞到Controller以及其余參數? ASP.Net MVC 5是否可以解決此問題?

通常,您將使用表單並將此數據發回,而不使用ActionLink。 但是,由於ActionLink基本上是<a> ,其HREF設置為操作方法URL,因此可以使用javascript附加它(例如,在HTML屬性中,如果提供ID為CmtLink,則):

$("#CmtLink").on("click", function() {
    var href = $(this).attr("href");
    //I assume it doesn't exist, but it's good to check for an existing comment parameter
    //also assume at least one parameter present
    //Also assume comment control is textbox, not textarea
    href += "&comment=" + $("#comment").val();

    $("#CmtLink").attr("href", href);
});

單擊時,這可以始終獲取輸入的最新值,或者如果注釋為空,則可以取消默認行為。

暫無
暫無

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

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