What happens during a controller method call? Does MVC for every PUBLIC method in your controller evaluate/set ModelState? Does it test EVERY class in the method parameters??
public ActionResult Create(Entity myEntity, AnotherEntity, myEntity2)
{
if (ModelState.IsValid)
{
If I had a return of int versus ActionResult:
public int Create(Entity myEntity, AnotherEntity, myEntity2)
{
if (ModelState.IsValid)
{
Would there still be a ModelState with the evaluated classes??
Actually it’s not the controller. It’s the model binder. The responsibility of the model binder is to instantiate the corresponding model given the request values. So the first step is model binding and the second step is validation. The first step is done by the model binder. If there’s an error during this step (for example you have attempted to bind an integer field on your model to an input text in which the user entered some arbitrary text), the model binder automatically adds an error to the model state, so once you enter the controller action you could test whether the
ModelState.IsValid.If model binding succeeds then you have an instance of the model that is now passed to the corresponding validation framework. So for example if you are using Data Annotations and decorated the model properties with validation attributes, they will be evaluated and once again if there are errors they will automatically be added to the ModelState.
You would violate standard conventions in ASP.NET MVC where all controller actions must return ActionResult. But the return type really has nothing to do with model binding of input parameters and validation. The return type could be any of the possible derived classes of ActionResult or a custom one.
So for example if you want to render the HTML representation of your model you return a
ViewResult. If you want to return the JSON representation of your model you return aJsonResult. If you want to return some static string you return aContentResult. If you want to allow the user download a file you return aFileResult. And so on.