Manually Set ModelState.isValid from Controller

前端 未结 1 1474
陌清茗
陌清茗 2021-01-01 08:38

Is there a way for me to manually set the ModelState.isValid = False from the controller?

I have some code like this

    Dim _region As          


        
1条回答
  •  鱼传尺愫
    2021-01-01 08:43

    You can't set ModelState.IsValid directly, as it's a derived property that simply checks the models error collection. You can however add your own model errors, e.g:

    ModelState.AddModelError("Region", "Region is mandatory");
    

    ModelState.IsValid will then return false.

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