[英]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 :) 希望有人能帮忙-这已经困扰了我大约2个小时-可能很简单:)
Kendo UI Grid sends a request to my controller 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
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 干杯克雷格
A few things. 一些东西。 Your immediate problem is that Company is mapped to a complex object not a primitive type. 您的直接问题是Company被映射到一个复杂的对象而不是原始类型。 Kendo Grid just does not do this (as of this writing). Kendo Grid只是不这样做(在撰写本文时)。 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. 只是猜测,但您可能想在网格上设置外键绑定,然后从列表框中传递回公司的ID。 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. 将后缀ViewModel用于绑定到View的模型,仅将后缀Model用于业务对象。 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. 这只是一个示例,最好让Ajax用IList加载网格,因为我假设您一次在网格中有很多用户,尽管您也可以将ViewModel与列表绑定在一起。 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. 但是“公司”列表每次都可能是相同的,因此只需将其映射到“视图”即可,而不是每次执行行编辑时都由Ajax加载。 (not always true) (并非总是如此)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.