MVC 2 View showing wrong model info

前端 未结 2 1349
[愿得一人]
[愿得一人] 2021-01-17 03:17

I\'m using MVC 2 for a project and I\'m having a problem with a view. In the controller I have the code:

return View(calendarDay);

If I deb

相关标签:
2条回答
  • 2021-01-17 03:57

    Seems like the problem is that the view is using the id from the controller and not the one from the model. I just changed the parameter name and works fine now.

    0 讨论(0)
  • 2021-01-17 04:05

    I suspect that you are trying to modify the POSTed value (which is 1) in your controller action to 2. This is not possible because that's how all HTML helpers work and it is by design: they will first look at the POSTed value when binding and after that in the model. So the HiddenFor helper ignores the Id of your model and uses the one that's posted.

    As a workaround you could:

    <input type="hidden" name="Id" value="<%: Model.Id %>" />
    

    As suggested by @jfar in the comments section another workaround is to clear the model state in the post action before returning the view:

    MoselState.Clear();
    
    0 讨论(0)
提交回复
热议问题