如何使用 ASP.Net MVC 在 Razor 视图中使用 Input 处理多个按钮?
How to handle multiple buttons with Input in Razor view Using ASP.Net MVC?
我看到很多post关于多按钮调用Action方法,但是那些都是没有Input的。
我有 Razor View。它将生成多个输入和按钮
例子
项目视图
@foreach (var item in ViewBag.Prod)
{
<input type="text" id="txtQuantity" class="form-control txtQty">
<button type="button" data-src="@(item.ItemID)" class="btn btn-primary btn-sm" onclick="location.href='@Url.Action("AddValue", "MyController")'" id="btnAddItem-@(item.ItemID)" style="margin-top: 15px;">Add</button>
}
So above code may generate, 5 textboxes and 5 Add button, every button click will call below action method.
How can I get the respective textbox value. For instance, if user enter value = 16.5 on 5th textbox, and click 5th button, I need to get the value of what user entered on that 5th textbox
ActionResult 方法
public ActionResult AddValue()
{
//I need to get the entered textbox value
return RedirectToAction("Item");
}
如果您只需要捕获特定行或行的输入,那么您可以在循环期间用表单标签将它们括起来。
编辑:不要忘记将名称属性添加到您的输入字段。我使用 Quantity
作为输入,使用 ID
作为按钮。然后将按钮更改为 type="submit"
并通过 value="@item.ItemID"
.
将 id 分配给它的值
@foreach (var item in ViewBag.Prod)
{
<form type="POST" action="@Url.Action('AddValue')">
<input type="text" name="Quantity" id="txtQuantity" class="form-control txtQty">
<button name="ID" value="@item.ItemID" type="submit" data-src="@(item.ItemID)" class="btn btn-primary btn-sm" id="btnAddItem-@(item.ItemID)" style="margin-top: 15px;">Add</button>
</form>
}
然后在你的控制器中,添加 [HttpPost]
属性,因为你没有使用任何模型,通过访问 Request[name]
.
获取提交的值
[HttpPost]
public ActionResult AddValue()
{
// input
var value = Request["Quantity"];
// button
var value = Request["ID"];
return RedirectToAction("Item");
}
我看到很多post关于多按钮调用Action方法,但是那些都是没有Input的。
我有 Razor View。它将生成多个输入和按钮
例子
项目视图
@foreach (var item in ViewBag.Prod)
{
<input type="text" id="txtQuantity" class="form-control txtQty">
<button type="button" data-src="@(item.ItemID)" class="btn btn-primary btn-sm" onclick="location.href='@Url.Action("AddValue", "MyController")'" id="btnAddItem-@(item.ItemID)" style="margin-top: 15px;">Add</button>
}
So above code may generate, 5 textboxes and 5 Add button, every button click will call below action method.
How can I get the respective textbox value. For instance, if user enter value = 16.5 on 5th textbox, and click 5th button, I need to get the value of what user entered on that 5th textbox
ActionResult 方法
public ActionResult AddValue()
{
//I need to get the entered textbox value
return RedirectToAction("Item");
}
如果您只需要捕获特定行或行的输入,那么您可以在循环期间用表单标签将它们括起来。
编辑:不要忘记将名称属性添加到您的输入字段。我使用 Quantity
作为输入,使用 ID
作为按钮。然后将按钮更改为 type="submit"
并通过 value="@item.ItemID"
.
@foreach (var item in ViewBag.Prod)
{
<form type="POST" action="@Url.Action('AddValue')">
<input type="text" name="Quantity" id="txtQuantity" class="form-control txtQty">
<button name="ID" value="@item.ItemID" type="submit" data-src="@(item.ItemID)" class="btn btn-primary btn-sm" id="btnAddItem-@(item.ItemID)" style="margin-top: 15px;">Add</button>
</form>
}
然后在你的控制器中,添加 [HttpPost]
属性,因为你没有使用任何模型,通过访问 Request[name]
.
[HttpPost]
public ActionResult AddValue()
{
// input
var value = Request["Quantity"];
// button
var value = Request["ID"];
return RedirectToAction("Item");
}