[英]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
中將數據從View
傳遞到Controller
。 帖子中的主要思想是
View
中的matching-model form-post(輸入)和Action Controller
name-convention-parameters,或 route values
與Action 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
的參數的View
和Action Controller
“中的S參數Controller
完美匹配, 除了一: comment
。
所有其他參數都可以在呈現時由“按剃刀View
呈現,但是comment
應該是用戶輸入的輸入(文本)。
現在,當按下“ Action Link
時,如何將comment
從View
傳遞到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.