且构网

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

Asp.Net MVC:为什么我的观点传递NULL模型回我的控制器?

更新时间:2023-02-12 13:18:16

其空,因为模型包含一个名为属性 gl_ code ,你也命名为你的模型参数 gl_ code 中的POST方法。

Its null because your model contains a property named gl_code and you have also named the parameter for your model gl_code in the POST method.

改变的一个或另一个的名称,模型将正确结合

Change the name of one or the other and the model will bind correctly.

什么是内部的动作是表单提交的每一个成功的表单控件的名称/值对,你的情况 gl_ code = someValue中。在 DefaultModelBinder 首先初始化模型的新实例。然后读取表单值和找到一个匹配模型中的属性,并将其设置为 someValue中。但它也找到一个匹配的方法参数和尝试设置参数为 someValue中的值,失败(因为你不能做 gl_ code gl_ code =someValue中; )和模型成为

What is happening internally is that the form submits a name/value pair for each successful form control, in your case gl_code=someValue. The DefaultModelBinder first initializes a new instance of your model. It then reads the form values and finds a match for the property in your model and sets it to someValue. But it also finds a match in the method parameters and tries set the value of the parameter to someValue, which fails (because you cannot do gl_code gl_code = "someValue";) and the model becomes null.