asp.netmodel-view-controllerentity-framework-4devexpressdevexpress-mvc

ASP.NET MVC UpdateModel is not working (with Devexpress)


I create a view, entity model and Devexpress Grid extension. Devexpress has self generate a code. But UpdateModal function is not working on controller function in class. throw a "The model of type 'Models.Birim' could not be updated." error text.

my codes:

[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());
        }

can it work on my work idea?


Solution

  • As orçun already pointed out, the DevExpress support has provided the necessary steps in the DevExpress forum.

    Quote from DevExpress forum:

    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;