Will ViewModel param never be null in ASP.NET MVC 3 action?

后端 未结 3 1414
死守一世寂寞
死守一世寂寞 2021-01-13 16:47

Will my viewModel param never be null in the following situation when the method is called through ASP.NET MVC? For example using the URL \".../Home/Index\".



        
3条回答
  •  遥遥无期
    2021-01-13 17:30

    MVC will try to take the values that were sent via a POST/GET and "automagically" bind them to the properties of the model. This is done by the default model binder. In most cases, the default model binder will do everything you need it to.

    You can look deeper into custom model binding (and might get a deeper understanding of it), but looking into the IModelBinder interface -- it contains a single method called BindModel. In this method, you would pretty much take whatever is in the ControllerContext.HttpContext.Request and fill in your class properties (basically the same thing the default model binder does). Using a custom model binder has a few more hoops you need to jump through, but will give you the idea of the binding lifecycle.

    The short answer: With the default model binder, it won't be null.

    HTH

提交回复
热议问题