我正在尝试构建 Telerik MVC 网格,并且在构建网格单元格中包含多个复选框的行时遇到了很大困难。
到目前为止,我的尝试只完成了一半。它在第一个单元格中显示一行带有一大堆复选框,但是当我想通过“插入”工具栏按钮添加记录时,我得到一个空行。
这是我的模型和观点。任何帮助,将不胜感激。
public class MyModel {
public MappingsModel[] Mappings { get; set; }
public class MappingsModel {
public CustomerRoleModel[] CustomerRoles { get; set; }
public int[] SelectedCustomerRoleIds { get; set; }
}
public class CustomerRoleModel {
public int Id { get; set; }
public string Name { get; set; }
public bool Selected { get; set; }
}
}
@(Html.Telerik().Grid(Model.Mappings)
.Name("rules-grid")
.DataKeys(keys =>
{
keys.Add(x => x.Id);
})
.DataBinding(dataBinding =>{
dataBinding.Ajax()
.Select("RuleList", "MyController")
.Insert("RuleInsert", "MyController")
.Update("RuleUpdate", "MyController")
.Delete("RuleDelete", "MyController");
})
.Columns(columns =>
{
columns.Bound(x => x.CustomerRoles)
.Template(@<text>
@foreach (var role in @item.CustomerRoles) {
<text><input type="checkbox" value="@(role.Id)" id="role_@(role.Id)" name="SelectedCustomerRoleIds" disabled="disabled" </text>
if(role.Selected) {
<text>checked="checked"</text>
}
<text>/><label for="role_(@role.Id)">@role.Name</label><br /></text>
}
</text>
)
.Width(500);
columns.Command(commands =>
{
commands.Edit();
commands.Delete();
})
.Width(180);
})
.ToolBar(commands => commands.Insert())
.EnableCustomBinding(true));
更新
我已经尝试过@howcheng 提供的建议,但这会使复选框如下所示:
[object Object],[object Object],[object Object],[object Object]
我通过 Ajax 返回的数据看起来是有效的,即使它在下面看起来不正确。有任何想法吗?
data: [{Name:null, Age:0,…}]
0: {Name:null, Age:0,…}
Age: 0
CustomerRoles: [{Name:Administrators, Selected:false, Id:1}, {Name:Forum Moderators, Selected:false, Id:2},…]
0: {Name:Administrators, Selected:false, Id:1}
1: {Name:Forum Moderators, Selected:false, Id:2}
2: {Name:Guests, Selected:false, Id:4}
3: {Name:Registered, Selected:false, Id:3}
Id: 1
Name: null
total: 1