繁体   English   中英

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

[英]how to get a value from textbox and pass it from actionlink to controller

我正在一个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,但没有得到任何结果,quantityWanted始终返回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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM