如何从文本框中获取值并将其从actionlink传递给控制器

时间:2017-08-06 14:15:03

标签: javascript asp.net-mvc razor html.textboxfor

我正在使用ASP.NET MVC应用程序,我需要将TextBox值从视图传递给控制器​​,这个TextBox在表中,我需要获取输入值。

    @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,但我没有得到任何东西,而且数量万事通常会返回null或0值。

1 个答案:

答案 0 :(得分: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>