asp.net mvc form includes for loop over form controllers

前端 未结 1 1582
无人共我
无人共我 2020-12-20 04:47

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

相关标签:
1条回答
  • 2020-12-20 05:06

    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" />

    0 讨论(0)
提交回复
热议问题