【问题标题】:MVC Kendo Grid multiselect columnMVC Kendo Grid 多选列
【发布时间】:2016-03-24 14:02:54
【问题描述】:

我有一个剑道 mvc 网格。

我希望其中一列是一个值列表。

例如我的数据对象:

public TestObject()
{
  public int TestID {get; set;}
  public string MyTest {get; set;}
  public string MyCategory {get; set;}
  public string Description {get; set;}
}

剑道网格:

@(Html.Kendo().Grid<MyProject.BusinessObjects.TestObject>()
        .Name("myGrid")
        .Columns(col => 
        {
            col.Bound(x => x.TestID);
            col.Bound(x => x.MyTest);
            col.Bound(x => x.MyCategory);
            col.Bound(x => x.Description);
            col.Command(x => { x.Edit(); x.Destroy(); });
        })
        .Selectable()
        .Scrollable()
        .ToolBar(x => x.Create())
        .Sortable()
        .Editable(e => e.Mode(GridEditMode.PopUp))
        .Pageable(p => p
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
        .DataSource(d => d
            .Ajax()
            .Read(r => r.Action("GetData", "MyController"))
            .PageSize(20)
            .Model(m => 
            {
                m.Id(x => x.TestID);                    
            })      
            .Update("UpdateData", "MyController")
            .Create("CreateData", "MyController")
            .Destroy("DeleteData", "MyController")
        )
   )

现在,当用户添加或编辑记录时,我希望 MyCategory 显示有效值列表。 (基本上 MyCategory 是一个 FK,但在 DB 中没有以这种方式强制执行)

如何使用 Kendo Grid 完成此任务?我试图遵循 Kendo 在线示例,但是当他们创建 ViewState 对象以及网格如何与该特定对象交互时,我错过了连接。
(剑道网格编辑自定义编辑器示例)

【问题讨论】:

    标签: c# asp.net-mvc telerik kendo-grid kendo-asp.net-mvc


    【解决方案1】:

    在 DB 中不必是 fk。

    columns.ForeignKey(p => p.MyCategory, (System.Collections.IEnumerable)ViewData["MyCategory"], "Id", "MyCategoryText");
    

    在控制器索引页面中,您必须填充视图数据。您可以从 DB 获取数据,也可以对值进行硬编码。

     public ActionResult Index()
        {            
            ViewData["MyCategory"] =  GetMyCategoryList();
            return View();
        }
    
     public List<SelectListItem> GetMyCategoryList()
        {
            List<SelectListItem> lstMyCategory = new List<SelectListItem>();
            var data = new[]{
                 new SelectListItem{ Id=1,MyCategoryText="Federal"},
                 new SelectListItem{ Id=2,MyCategoryText="General"},
                 new SelectListItem{ Id=3,MyCategoryText="Cash"},                 
             };
            lstMyCategory = data.ToList();
            return lstMyCategory;
        }
    

    【讨论】:

    • 这几乎达到了我想要做的。它强制执行数据,因此用户无法保存 rouge 类别。但是,从添加/编辑中,我希望类别字段成为这些值的下拉列表,就像在剑道示例中一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多