如何从文本框中获取值并将其从 actionlink 传递给控制器
how to get a value from textbox and pass it from actionlink to controller
我正在开发一个 ASP.NET MVC 应用程序,我需要将 TextBox 值从视图传递到控制器,这个 TextBox 在 table 中,我需要获取输入值。
@foreach (var item in Model)
{
<tr class="odd gradeX">
<td>
@Html.DisplayFor(modelItem => item.Product.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product.Reference)
</td>
<td>
@Html.TextBoxFor(modelItem => item.QuantityWanted, new { style = "width:60%", type = "number", min = "0", step = "1", max = item.Quantity })
@Html.ActionLink("Add", "Add", new { idProduct = item.ProductID, idUser = item.UserID, quantityWanted= item.QuantityWanted })
</td>
</tr>
}
我使用了 javascript 和 jquery 但我没有得到任何东西,而且 quantityWanted 总是 returns null 或 0 值。
我想你想要这样的代码:
@foreach (var item in Model)
{
<tr class="odd gradeX">
<td>
@Html.DisplayFor(modelItem => item.Product.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product.Reference)
</td>
<td>
<input type="number" id="Quantity@item.ProductID" data-idUser = "@item.UserID" style="width:60%" min="0" step="1" max="@item.Quantity"/>
<button click="AddItem(@item.ProductID)">Click to add</button>
</td>
</tr>
}
<script>
function AddItem(id){
var quantity = $('Quantity' + id).val();
window.location = "/Add/Add?idProduct" + id + "&idUser=" + $('Quantity' + id).data("idUser") + "&quantityWanted=" + quantity;
}
</script>
我正在开发一个 ASP.NET MVC 应用程序,我需要将 TextBox 值从视图传递到控制器,这个 TextBox 在 table 中,我需要获取输入值。
@foreach (var item in Model)
{
<tr class="odd gradeX">
<td>
@Html.DisplayFor(modelItem => item.Product.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product.Reference)
</td>
<td>
@Html.TextBoxFor(modelItem => item.QuantityWanted, new { style = "width:60%", type = "number", min = "0", step = "1", max = item.Quantity })
@Html.ActionLink("Add", "Add", new { idProduct = item.ProductID, idUser = item.UserID, quantityWanted= item.QuantityWanted })
</td>
</tr>
}
我使用了 javascript 和 jquery 但我没有得到任何东西,而且 quantityWanted 总是 returns null 或 0 值。
我想你想要这样的代码:
@foreach (var item in Model)
{
<tr class="odd gradeX">
<td>
@Html.DisplayFor(modelItem => item.Product.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product.Reference)
</td>
<td>
<input type="number" id="Quantity@item.ProductID" data-idUser = "@item.UserID" style="width:60%" min="0" step="1" max="@item.Quantity"/>
<button click="AddItem(@item.ProductID)">Click to add</button>
</td>
</tr>
}
<script>
function AddItem(id){
var quantity = $('Quantity' + id).val();
window.location = "/Add/Add?idProduct" + id + "&idUser=" + $('Quantity' + id).data("idUser") + "&quantityWanted=" + quantity;
}
</script>