ASP.NET MVC: ModelState vs. ModelStateDictionary

前端 未结 6 1692
予麋鹿
予麋鹿 2021-02-04 09:32

I have a service which has a method that\'s called when a certain controller method is triggered.

My service returns a custom result object PlacementResult in w

6条回答
  •  悲哀的现实
    2021-02-04 09:57

    Your PlacementResult should return a dictionary object or a List which you should merge with the model state at the beginning of each action.

    If you step through you will notice the controllers model state dictionary contains all your input fields, their values and the errors associated with them. You want to merge the PlacementResult errors into the model state dictionary at the appropriate keys. This is how the view engine knows which fields to flag as invalid.

    ModelState.Merge(PlacementResult);
    if(ModelState.IsValid)
    {
        ...
    }
    

提交回复
热议问题