none
ASP.NET MVC TryValidateModel() Issues when Model is Modified RRS feed

  • Question

  • When page gets posted, ModelState of a particular view model is false because username field of that particular model was not set (not entered) on the page. But after setting username and trying to validate model by calling TryValidateModel() but it actually return false instead of returning true. 

    So I want to understand why does TryValidateModel still return false when mode is modified (i.e. model updated with username value in this case)? Could you please suggest?

    Friday, February 7, 2020 10:36 PM

All replies

  • There is an ASP.NET MVC forum in ASP.NET forums.

    http://forums.asp.net/

    Saturday, February 8, 2020 12:47 AM
  • Hi,

    The DefaultModelBinder of model binding validates your action parameters, and TryValidateModel it doesn't clear the ModelState so the validation errors remain there. You need to clear the ModelState collection if you want to redo the validation later manually.

    Please refer to this link: ASP.NET MVC TryValidateModel() Issues when Model is Modified

    Best Regards,

    Timon



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 10, 2020 9:44 AM