I have a table:
<% using(Html.BeginForm("View2","Order"))
{ %>
<table id="Products" class="Products">
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
<% for(int i=0; i < Model.NorthOrderDetails.Count; i++)
{ %>
<tr>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductID.ToString()) %></td>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductName) %> </td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity) %></td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice) %></td>
<td><button type="button" class="delete" data-id="<%:Model.NorthOrderDetails[i].ProductID %>">Delete</button><td>
<td><input type="hidden" name="<%:Model.NorthOrderDetails[i].ProductName %>" value="<%:i %>" /><td>
<tr>
<% } %>
</table>
<input type="submit" name="button" value="Add" />
<input type="submit" name="button" value="Save" />
<% } %>
When I click the delete button I'm calling this script:
<script type="text/javascript">
var url = '<%:Url.Action("Delete", "Order")%>';
$('.delete').click(function () {
var id = $(this).data('id'); // Get the product ID
var row = $(this).closest('tr');// Get the table row
$.post(url, { ID: id }, function () {
row.remove(); // remove the row from the table
});
});
</script>
And the script call this method in the Controller
[HttpPost]
public JsonResult Delete(int ID)
{
NorthOrder forOrderDetail = (NorthOrder)Session["Order"];
forOrderDetail.NorthOrderDetails.RemoveAll(z => z.ProductID == ID);
Session["Order"] = forOrderDetail;
return Json(null);
}
In the UI the row deletes correctly when I click on the submit button. But In the controller method the count of the collection equal null when I delete the first row, and it's equals to 1 when I delete the last row For example, table contains two rows when page loaded
public ActionResult View2(NorthOrder q, string button)
{
}
Why?