i want to submit a form with for loop inside of it that loops over the form controllers since i want to pass list of model into a post method action in such a way that each
You posting back the whole collection, so all the property values have been added to ModelState
, and the HtmlHelper
methods that generate form controls (except PasswordFor()
) use the values from ModelState
(if they exist) rather the the property value.
You can solve this by adding
ModelState.Clear();
before you return the view, however the correct approach is to follow the PRG (Post, Redirect Get) pattern, and redirect to your GET method.
To understand why the HtmlHelper
methods use ModelState
, refer this answer.
As a side note, you can improve performance by using ajax to remove the item (and if successfully deleted, remove the corresponding row from the DOM), but that means you also need to include a hidden input for the collection indexer so that you can post back non-zero/non-consecutive indexers if your also posting back the updated collection elsewhere in your view. The input would be <input type="hidden" name="Index" value = "@i" />