0

I've added a form to my default Index view with an added column for a Html.TextBox named 'Quantity'. When I hit Submit I'm sending it to the Action "OrderForm" but when I debug I only pass the Quantity value and the itemName and inventory_no is null. Can anyone help?

enter image description here

Below is sample code from my MVC:

Model:

[Table("inventory")] public partial class inventory { [Key] [Column(Order = 0)] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int inventory_no { get; set; } public string name { get; set; } } 

View:

 @model IEnumerable<MYAPP.inventory> @{ ViewBag.Title = "Index"; } @using (Html.BeginForm("orderForm", "inventories", FormMethod.Post)) { <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <div id="details"></div> <table class="table"> <tr> <th> Quantity </th> <th> Name </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.TextBox("Quantity",0, new { style = "width:30px", @class = "selectedItem"}) </td> <td class="itemName"><b> @Html.DisplayFor(modemItem => item.name) </b> </td> <td> <a href="#" class="remove" data-url="@Url.Action("Remove", new { id = item.inventory_no })">Remove</a> @Html.ActionLink("Edit", "Edit", new { inventory_no = item.inventory_no.ToString(), client_no = item.client_no }) | @Html.ActionLink("Delete", "Delete", new { inventory_no = item.inventory_no.ToString(), client_no = item.client_no }) </td> </tr> } </table> <input type="submit" value="Submit your order" /> } 

Controller :

 public ViewResult orderForm (int quantity, string itemName, int? inventory_no) { return View(); } 
2
  • You only have textbox for one field? Commented Jan 3, 2017 at 0:57
  • puu.sh/t9dNL/3fb3950293.png Commented Jan 3, 2017 at 1:02

1 Answer 1

1

Your current code is generating markup for only quantity. If you need the inventory_id and name values, you need to have form fields for that.

You can keep these values in hidden field with names matching to your action method parameter. Also since your action method takes a single inventory items details, you should have your form and submit button for each item in the loop

@foreach (var item in Model) { using(Html.BeginFor("orderForm","YourcontrollerName")) { <tr> <td> @Html.TextBox("Quantity",0, new { @class = "selectedItem"}) </td> <td class="itemName"> @Html.DisplayFor(modemItem => item.name) @Html.Hidden("name",item.name) @Html.Hidden("inventory_no",item.inventory_no) </td> <td> <input type="submit" value="Submit your order" /> <!-- your existing links here--> </td> </tr> } } 
Sign up to request clarification or add additional context in comments.

2 Comments

I'm new to ASP.NET MVC. I wasn't aware about the HiddenFor HTML Helper.
Use Hidden helper(I updated the answer). It basically generate the markup for hidden elements (which has your values) in the form so that it is available on form submit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.