且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将视图模型参数永远不会在ASP.NET MVC 3行动空?

更新时间:2023-11-29 16:57:28

是的,你总是会得到一个视图模型的实例,偶虽然没有任何参数存在可分配给视图模型的请求。

Yes you will always get an instance of a view model even-though there is no any parameter in exists in the requests that can be assigned to the view model.

为什么出现这种情况?

这是怎么了 DefaultModelBinder


  1. 创建视图模型的实例。

  1. Create an instance of the view model.

迭代模型中的属性,并要求该值提供商发现值,并设置找到的值的属性

Iterate the properties in the model and ask the value providers to find values and set the found values to the properties.

返回创建的实例。

如果您看到的步骤 DefaultModelBinder 它并不关心是否有请求可被设置为模型实例,它只是提前启动,并创造任何价值实例,然后填写属性。

If you see the steps the DefaultModelBinder it doesn't care if there is any value in the request that can be set to the model instance it's just start ahead and create the instance and then fill the properties.

这没有什么意义吧?

这将是更加复杂的模型绑定来检查请求是否含有该模型的属性相匹配的值,然后创建一个实例。

It would be more complex for the model binder to check the request whether it contains any value that matches the property of the model and then create an instance.