I have a table: <% using(Html.BeginForm('View2','Order')) { %>
ProductId
&
Solution 1:
Because you are deleting some rows, but posting back the whole collection in the Save submit action, your indexers are either not starting a zero, or are non consecutive. In the case of deleting the 1st row, the posted values are
There are no values with NorthOrderDetails[0]... so binding fails and the collection is empty.
By default, the DefaultModelBinder require collection indexers to start at zero and be non-consecutive. When you delete an item and remove its controls from the DOM, the collection cannot be bound correctly. To make this work you need to add an extra hidden input which the DefaultModelBinder uses to match up collection properties. Change the view code to
Note: I have long forgotten aspx, but the razor code for the hidden input is <input type="hidden" name="NorthOrderDetails.Index" value="@i" /> so you may want to check my syntax.
Side notes:
Your use of session seems inappropriate here. In the Delete
method, you are removing the item. When your finally save the
collection, you would need to get the original collection from the
database and compare them to determine which items to delete in the
database. Instead, you Delete() method should be calling the
database to delete the item (refer my answer to your previous
question)
Your Add button should not be, a submit and posting to the
View2() method.
Share
Post a Comment
for "Jquery Script Delete All Items In Form's Collection"
Post a Comment for "Jquery Script Delete All Items In Form's Collection"