MVC参数未绑定到控制器操作(KENDO UI)

时间:2013-03-19 22:03:18

标签: asp.net-mvc kendo-ui

希望有人可以提供帮助 - 这已经困扰了我大约2个小时 - 这可能很简单:)

Kendo UI Grid向我的控制器发送请求

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

但是,控制器类'Company'不受绑定器约束?任何人都可以帮助我的视图模型和控制器操作签名如下:

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

干杯 克雷格

1 个答案:

答案 0 :(得分:1)

一些事情。您当前的问题是公司被映射到一个复杂的对象而不是一个原始类型。 Kendo Grid不会这样做(截至撰写本文时)。只是猜测,但你可能想在网格上设置一个外键绑定,只是从列表框中传回公司的Id。这并不像你想象的那么糟糕,它会立即解决你的问题并且看起来也不错。

也许是个人品味,但似乎是一种惯例。将后缀ViewModel用于绑定到View的模型,并使用后缀Model作为业务对象。因此,Kendo Grid总是填充模型。

例:

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!这只是一个例子,你最好用Ajist加载网格,因为我假设网格中有很多用户,尽管你也可以通过服务器绑定ViewModel。但是公司列表每次都可能是相同的,所以将它映射到View只是谎言而不是每次进行行编辑时Ajax加载它。 (并非总是如此)

相关问题