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++)
{%>
<%: Html.HiddenFor(m => m.NorthOrderDetails[i].ProductID) %>
<%: Html.HiddenFor(m => m.NorthOrderDetails[i].ProductName) %>
<tr>
<td><%: Html.DisplayFor(m => m.NorthOrderDetails[i].ProductID) %></td>
<td><%: Html.DisplayFor(m => m.NorthOrderDetails[i].ProductName) %></td>
<td>
<input type="hidden" name="NorthOrderDetails.Index" value="<%: i %>" />
<%: 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="<%: Model.NorthOrderDetails[i].Quantity %>" /></td>
</tr>
<% } %>
</table>
<input type="submit" name="button" value="Add" />
<input type="submit" name="button" value="Save" />
<% } %>
<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 when I click the delete button I'm calling this script:
});
});
</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);
}
When I click the add button I'm calling this method in Controller:
[HttpPost]
public ActionResult View2(NorthOrder q, string button)
{
string strDDLValue = Request.Form["sda"].ToString();
int drop = Convert.ToInt32(strDDLValue);
NorthOrder forOrderDetail = (NorthOrder)Session["Order"];
if (button == "Add")
{
NorthwindEntities n = new NorthwindEntities();
string strDDLValuse = n.Products.FirstOrDefault(_ => _.ProductID == drop).ProductName;
NorthOrderDetailscs orddetailForSession = new NorthOrderDetailscs();
orddetailForSession.ProductName = strDDLValuse;
orddetailForSession.Quantity = 0;
orddetailForSession.UnitPrice = 0;
orddetailForSession.ProductID = drop;
forOrderDetail.NorthOrderDetails.Add(orddetailForSession);
Session["Order"] = forOrderDetail;
}
return View(forOrderDetail); //
}
and problem concludes displaying data in new row in textboxes from nondeleting item, when I deleted the first item, and after I added new item. Although, in model adding item contains null values for textboxes
In input hidden field Model.NorthOrderDetails[i].Quantity = 0
in new item, as in debug as in markup, but in UI in textbox new item in textbox contains values from existing item
For example, in table two rows
ProductID | ProductName| Quantity| UnitPrice
_____________________________________________
17 | Chang | 1 | 12
_____________________________________________
12 | Chai | 2 | 24
When I delete first row, and then I add new row I get table
ProductID | ProductName| Quantity| UnitPrice
_____________________________________________
12 | Chai | 2 | 24
_____________________________________________
15 | Allice Mutton | 2 | 24
What is reason of it?