Craig Hannon
Craig Hannon

Reputation: 4069

MVC parameter not binding to controller action (KENDO UI)

Hope someone can help - this has been bugging me for around 2 hours - its probably something simple :)

Kendo UI Grid sends a request to my controller

http://localhost:1418/user/update?UserID=1&UserName=Admin&RoleName=Admin&Email=c.j.hannon%40gmail.com&Active=true&Company%5BCompanyID%5D=1&Company%5BCompanyName%5D=asd

However, the controller class 'Company' isnt bound by the binder? Can any one help my view model and controller action signature are below:

[HttpGet]
        public JsonResult Update(UserViewModel model)
        {
            svcUser.UpdateUser(new UpdateUserRequest() {
                UserID=model.UserID,
                RoleID = model.RoleName,
                Email = model.Email,
                Active = model.Active.GetValueOrDefault(false),
                UserName = model.UserName
            });

            return Json("", JsonRequestBehavior.AllowGet);
        }

public class UserViewModel
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string RoleName { get; set; }
        public string Email { get; set; }
        public bool? Active { get; set; }
        public CompanyViewModel Company { get; set; }
    }

Cheers Craig

Upvotes: 0

Views: 908

Answers (1)

Trey Gramann
Trey Gramann

Reputation: 2004

A few things. Your immediate problem is that Company is mapped to a complex object not a primitive type. Kendo Grid just does not do this (as of this writing). Just guessing, but you probably want to setup a foreign key binding on the Grid and just pass back the Id of the company from a listbox. This is not as bad as you think and it will immediatly fix your problem and look nice too.

Maybe personal taste but seems to be a convention. Use the suffix ViewModel for the model that is bound to your View and just the suffix Model for your business objects. So a Kendo Grid is always populated with a Model.

Ex.:

public class UserModel
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string RoleName { get; set; }
    public string Email { get; set; }
    public bool? Active { get; set; }
    public int CompanyID { get; set; }
}
public class CompanyModel
{
    public int ID { get; set; }
    public string Name { get; set; }
}
public class UserViewModel
{
    public UserModel UserModel { get; set; }
    public IList<CompanyModel> Companies { get; set; }
}

public ActionResult UserEdit(string id)
{
    var model = new UserViewModel();
    model.UserModel = load...
    model.Companies = load list...
    return View(model);
}

@model UserViewModel
...
column.ForeignKey(fk => fk.CompanyId, Model.Companies, "ID", "Name")
(Razor Notation)

BUT! This is just an example, you are better off Ajax loading the Grid with the IList becuase I assume you have many Users in the Grid at once, though you could server bind off the ViewModel with a List too. But the list of Companies is probably the same every time, so map it to the View just liek this rather than Ajax load it every time you do a row edit. (not always true)

Upvotes: 1

Related Questions