ASP.NET MVC: ModelState vs. ModelStateDictionary

前端 未结 6 1675
予麋鹿
予麋鹿 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) {...
    
    0 讨论(0)
  • 2021-02-04 09:55

    This is a good link that shows how a service can perform validation and communicate the result back to the controller:

    http://www.asp.net/mvc/tutorials/validating-with-a-service-layer-cs (fixed link)

    0 讨论(0)
  • 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)
    {
        ...
    }
    
    0 讨论(0)
  • 2021-02-04 10:01

    No, you do not want to add a ModelStateDictionary to your result type. There is already a ModelStateDictionary on the Controller (in the ModelState property). It is not appropriate for results to set the controller's model state. That should be done during binding or within the controller action itself. Use a custom model binder if you need to.

    Your choose one can see the model state errors by examining the controller's ViewData.ModelState property.

    0 讨论(0)
  • 2021-02-04 10:01

    I don't know what your PlacementResult looks like, see if you can possibly utilize this in your view:

    ModelState.AddModelError(ErroredProperty, ErrorMessage);
    

    Make sure you return the object that failed back to the view

    return View(myObjectInstance);
    
    0 讨论(0)
  • 2021-02-04 10:02

    Based on SoC I think you have to return errors from your services and merge them in your ModelState if needed.

    But our objective is maintain decoupling and also to use ModelState.Merge() method. it isn't?

    There is an concrete implementation that could help

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