简体   繁体   中英

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

I'm working on an ASP.NET MVC application and i need to pass TextBox value from view to controller this TextBox is in table and i need get the input value.

    @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>
    }

I used javascript and jquery but i didn't get anything and quantityWanted always returns null or 0 value.

I think you want code like this:

@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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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