javascriptasp.net-mvcrazorhtml.textboxfor

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.


Solution

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