ASP.NET MVC: ModelState vs. ModelStateDictionary

前端 未结 6 1686
予麋鹿
予麋鹿 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:43

    You can pass the Controller to your method, since the Controller class contains the ModelState property. Once in your method you can do the following:

    private PlacementResult BuildResult(Controller controller)
    {
       controller.ModelState.AddModelError(propertyName, errorMessage);
    }
    

    In your action...

    BuildResult(this);
    if(ModelState.IsValid) {...
    

提交回复
热议问题