我将创建一个网页来使用ASP.NET MVC Kendo Grid管理我的用户。当我单击Kendo Grid中的Add New按钮时,没有任何事情发生,按钮不起作用。我该如何解决这个问题呢?
这是我的UserViewModel类:
public class UserViewModel
{
public int Id { get; set; }
public string Name { get; set; }
[ScriptIgnore]
public byte[] Photo { get; set; }
public string Photo64
{
get
{
return Photo != null ? Convert.ToBase64String(Photo) : null;
}
}
public bool Leave { get; set; }
}
以下是我的阅读和创建操作:
public ActionResult Users_Read([DataSourceRequest]DataSourceRequest request)
{
using (var db = new DBEntities())
{
var users = from u in db.Users
select new UserViewModel
{
Id = u.Id,
Name = u.Name,
Photo = u.Photo,
Leave = u.Leave ?? false
};
DataSourceResult result = users.ToDataSourceResult(request);
return Json(result);
}
}
public ActionResult Users_Create([DataSourceRequest]DataSourceRequest request, UserViewModel user)
{
if (ModelState.IsValid)
{
using (var db = new DBEntities())
{
var entity = new User
{
Name = user.Name
Photo = user.Photo,
Leave = user.Leave
};
db.Users.Add(entity);
db.SaveChanges();
user.Id = entity.Id;
}
}
return Json(new[] { user }.ToDataSourceResult(request, ModelState));
}
这是我的观点:
@(Html.Kendo().Grid<MyApp.Models.User>()
.Name("grid")
.Columns(c =>
{
c.Bound(u => u.Id).Visible(false);
c.Bound(u => u.Photo).ClientTemplate(
"# if (Photo64 == null) { #" +
"<img src='" + Url.Content("~/Images/") + "no_profile_photo.jpg' />" +
"# } else { #" +
"<img src='data:image/jpg;base64,#=Photo64#' />" +
"# } #"
);
c.Bound(u => u.Name);
c.Bound(u => u.Leave).ClientTemplate(
"<input type='checkbox' onclick='return false' value='#= Id #' " +
"# if (Leave) { #" +
"checked='checked'" +
"# } #" +
"/>"
);
c.Command(cmd => { cmd.Edit(); cmd.Destroy(); });
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.DataSource(dataSource => dataSource
.Ajax()
.Model(m =>
{
m.Id(user => user.Id);
m.Field(user => user.Name);
m.Field(user => user.Photo);
m.Field(user => user.Leave);
})
.Create(create => create.Action("Users_Create", "User"))
.Read(read => read.Action("Users_Read", "User"))
.Update(update => update.Action("Users_Update", "User"))
.Destroy(destroy => destroy.Action("Users_Destroy", "User"))
)
.Pageable()
.Sortable()
.Filterable()
)
答案 0 :(得分:1)
您的Users_Read方法返回类型为UserViewModel的kendo数据源,但网格定义为Grid&lt; MyApp.Models.User&gt;类型。这导致错误。