[英]I want to send data from javascript to an MVC controller and switch window to IActionResult of that controller
[英]I want to send the textbox value to the controller in MVC
我在這樣的視圖中有一個文本框
< input type="text" id="Quant" value="@item.Quantity"/>
當文本框值更改時,我想將此文本框的值發送到操作方法。
我的動作方法是這樣的
public ActionResult Quant(int id)
{
int index = isExisting(id);
List<Item> cart = (List<Item>)Session["cart"];
cart[index].Quantity= id;
Session["cart"] = cart;
return View("Cart");
}
我希望將文本框值發送到actionresult Quant的參數ID。 我們是否可以為此使用任何javascript或jquery。 請幫助我。 提前致謝。
首先,您需要弄清楚何時要將文本值發送到控制器。 當文本框更改時或在文本框的鍵入或類似內容上。 我可能會將其放在更改文本框中,您可以在輸入時使用它。 然后,您可以運行一些ajax代碼,如下所示:
$('#Quant').on("input propertychange paste", function() {
$.ajax({
url: '/Quant',
data: { 'id' : $('#Quant').val() },
type: "post",
cache: false,
success: function () {
alert('success !');
}
});
});
只需修改您的代碼
第一個輸入框:
<input type="text" id="Quant" name ="Quant" value="@item.Quantity"/>
第二種正確的操作方法:
public ActionResult Quant(String Quant)
{
int _Quant = Convert.ToInt16(Quant);
int index = isExisting(id);
List<Item> cart = (List<Item>)Session["cart"];
cart[index].Quantity= id;
Session["cart"] = cart;
return View("Cart");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.