如果参数被命名为“模型",则模型绑定到动作始终为空.用于剑道 ui 网格创建操作 [英] Model binding to action is always null if the parameter is named "model" for kendo ui grid create actions

查看:11
本文介绍了如果参数被命名为“模型",则模型绑定到动作始终为空.用于剑道 ui 网格创建操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个操作,其中请求是从 kendo ui 网格的 Create 事件生成的.信息全部正确发布,并且模型绑定器在动作参数名称为模型"的任何情况下都能正常工作

I have an action where the request is being generated from a kendo ui grid's Create event. The information is all being posted correctly, and the model binder works properly expect in any case where the action parameter name is 'model'

如果动作是这样定义的:

If the action is defined like this:

[HttpPost]
public ActionResult Create(ModelType post)
{                                                                                                    
}

一切正常.

如果相反,操作看起来像这样:

If instead though, the action looks like this:

[HttpPost]
public ActionResult Create(ModelType model) //changed parameter Name to model
{                                                                                                    
}

我收到无效的模型状态和此消息:

I get an invalid model state and this message:

System.Web.Mvc.ModelError

System.Web.Mvc.ModelError

从类型'System.String'到类型的参数转换'MyApp.Common.Models.ModelType' 失败,因为没有类型转换器可以在这些类型."

The parameter conversion from type 'System.String' to type 'MyApp.Common.Models.ModelType' failed because no type converter can convert between these types."

这似乎只影响来自 Kendo UI 网格的操作,无论操作参数的名称如何,正常的 MVC 操作帖子都可以工作.

This only seems to affect actions coming from the Kendo UI grid, normal MVC action posts work regardless of the name of the action parameter.

我能够用一个小的测试模型创建一个新项目并重新创建这种行为.

I was able to create a new project with a small test model and recreate this behavior as well.

有人能解释一下这里发生了什么吗?

Can anyone shine some light on what is going on here?

推荐答案

很可能当前的ModelType"包含名称为Model"的属性,并且默认 MVC ModelBinder 尝试将Model"属性绑定到模型变量.您应该将动作参数中的变量名称更改为当前模型中不包含的变量名称.

Most probably the current "ModelType" contains property with name "Model" and the Default MVC ModelBinder tries to bind the "Model" property to the model variable. You should change the variable name from the action parameters to one that is not contained in the current model.

这篇关于如果参数被命名为“模型",则模型绑定到动作始终为空.用于剑道 ui 网格创建操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆