ASP.NET MVC UpdateModel 不工作(使用 Devexpress)
ASP.NET MVC UpdateModel is not working (with Devexpress)
我创建了一个视图、实体模型和 Devexpress 网格扩展。
Devexpress 已自行生成代码。但是 class 中的 UpdateModal 函数不适用于控制器函数。
抛出 "The model of type 'Models.Birim' could not be updated." 错误文本。
我的代码:
[HttpPost, ValidateInput(false)]
public ActionResult MagazaGridPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] Models.Birim item)
{
var model = db.Birim;
if (ModelState.IsValid)
{
try
{
var modelItem = model.FirstOrDefault(it => it.id == item.id);
if (modelItem != null)
{
this.UpdateModel(modelItem);
db.SaveChanges();
}
}
catch (Exception e)
{
ViewData["EditError"] = e.Message;
}
}
else
ViewData["EditError"] = "Please, correct all errors.";
return PartialView("_MagazaGridPartial", model.ToList());
}
它能实现我的工作想法吗?
正如 orçun 已经指出的,DevExpress 支持已在 DevExpress 论坛中提供了必要的步骤。
So that the post data of our controls is applied properly, our
DevExpressEditorsBinder should be used. The UpdateModel method does
not use a binder specified in the ModelBinder attribute of an action
method. It's necessary to specify it as the default binder.
protected void Application_Start()
{
...
ModelBinders.Binders.DefaultBinder = new DevExpressEditorsBinder();
}
If you don't want this, you can get new values from the "item"
parameter of the GridViewPartialUpdate action method:
modelItem.LoginUsuario = item.LoginUsuario;
modelItem.Nome = item.Nome;
modelItem.Email = item.Email;
modelItem.Ativo = item.Ativo;
我创建了一个视图、实体模型和 Devexpress 网格扩展。 Devexpress 已自行生成代码。但是 class 中的 UpdateModal 函数不适用于控制器函数。 抛出 "The model of type 'Models.Birim' could not be updated." 错误文本。
我的代码:
[HttpPost, ValidateInput(false)]
public ActionResult MagazaGridPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] Models.Birim item)
{
var model = db.Birim;
if (ModelState.IsValid)
{
try
{
var modelItem = model.FirstOrDefault(it => it.id == item.id);
if (modelItem != null)
{
this.UpdateModel(modelItem);
db.SaveChanges();
}
}
catch (Exception e)
{
ViewData["EditError"] = e.Message;
}
}
else
ViewData["EditError"] = "Please, correct all errors.";
return PartialView("_MagazaGridPartial", model.ToList());
}
它能实现我的工作想法吗?
正如 orçun 已经指出的,DevExpress 支持已在 DevExpress 论坛中提供了必要的步骤。
So that the post data of our controls is applied properly, our DevExpressEditorsBinder should be used. The UpdateModel method does not use a binder specified in the ModelBinder attribute of an action method. It's necessary to specify it as the default binder.
protected void Application_Start()
{
...
ModelBinders.Binders.DefaultBinder = new DevExpressEditorsBinder();
}
If you don't want this, you can get new values from the "item" parameter of the GridViewPartialUpdate action method:
modelItem.LoginUsuario = item.LoginUsuario;
modelItem.Nome = item.Nome;
modelItem.Email = item.Email;
modelItem.Ativo = item.Ativo;