Model binding to action is always null if the parameter is named “model” for kendo ui grid create actions

折月煮酒 提交于 2019-12-22 18:13:40

问题


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)
{                                                                                                    
}

Everything works properly.

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

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/15549495/model-binding-to-action-is-always-null-if-the-parameter-is-named-model-for-ken

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!