ASP.NET MVC4, View returning old values to controller

前端 未结 1 1288

I am new to MVC and ASP.NET. My requirement is, I have to display two records in my View for the firsttime and my ViewContains one \'SWAP\' button. When I press this button,

相关标签:
1条回答
  • 2021-01-18 15:47

    Because you are returning the same model from a post, ASP.Net MVC is assuming that you have errors that you want to present back to the user (so it retains original values). You can fix this by either clearing the model state for the entire model, or clearing the model state for one or more fields. See below. This will be done, of course, in your controller.

    ModelState.Clear(); //clear entire model state
    ModelState.Remove("MyObject.MyProperty"); //clear only one property
    

    Rick Strahl has a good explanation of this issue on his blog: ASPNET-MVC-Postbacks-and-HtmlHelper-Controls-ignoring-Model-Changes

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