使用DropdownList的Kendo Grid

时间:2013-06-19 07:28:46

标签: asp.net asp.net-mvc-3 grid kendo-ui

您好我正在使用带有下拉列表的Kendo网格,我按照http://demos.kendoui.com/web/grid/foreignkeycolumn.html中的演示进行了操作 但我收到此错误“DataBinding:'System.Web.Mvc.SelectListItem'不包含名为'Id'的属性。”

任何人都可以帮助我让代码正常运行吗?

这是我的viewModel:

public class AccountingViewModel

{
    private string _code = string.Empty;

    private string _description = string.Empty;

    public int Id
    {
        get;set;
    }

    public string Code
    {
        get { return _code; }
        set { _code = value; }
    }

    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }

    public int MajorCategoryId
    {
        get;set;
    }

    public SelectList MajorCategories
    {
        get;set;
    }
}

这是我的控制器:

 public ActionResult Index()

    {
        var majorCategory = new SelectList(new[]
                                      {
                                          new {Id="1",Name="Category1"},
                                          new{Id="2",Name="Category2"},
                                          new{Id="3",Name="Category3"},
                                      },
                        "Id", "Name", 1);

        ViewData["majorCategories"] = majorCategory;

        return View(accountingService.GetAllAccountings());

    }

这是我的索引视图:

 @model IEnumerable < PPMS.Model.ViewModels.AccountingViewModel >
 < br/ >

< div class="k-grid" >

 @(Html.Kendo().Grid(Model)
.Name("grid")
.Columns(columns =>
       {
             columns.Bound(p => p.Code);
             columns.Bound(p => p.Description).Width(150);
              columns.ForeignKey(p => p.MajorCategoryId, (System.Collections.IEnumerable)ViewData["majorCategories"], "Id", "Name")
                           .Title("MajorCategory").Width(150);

         columns.Command(command => command.Destroy()).Width(110);
                   })
      .ToolBar(toolBar =>
                   {
                       toolBar.Save();
                       toolBar.Create();
                   })
      .Editable(editable => editable.Mode(GridEditMode.InCell))
      .Filterable()
      .Groupable()
      .Pageable()     
      .Scrollable()
      .HtmlAttributes(new { style = "height:430px;" })    
      .DataSource(dataSource => dataSource
                    .Ajax()
                    .Batch(true)
                    .PageSize(20)
                    .ServerOperation(false)
                    .Events(events => events.Error("errorHandler"))
                     .Model(model =>
                                    {
                                        model.Id(p => p.Id);
                                        model.Field(p => p.Id).Editable(false);
                                   model.Field(p => p.MajorCategoryId).DefaultValue(1);       
                                               })

                       .Create(create => create.Action("Create", "Accounting"))
                       .Read(read => read.Action("Index", "Accounting"))  
                       .Update(update => update.Action("Edit", "Accounting")) 
                       .Destroy(destroy => destroy.Action("Delete", "Accounting"))
       )
      )
</div>
<br/>


<script type="text/javascript">


function errorHandler(e) {
    if (e.errors) {
        var message = "Errors:\n";
        $.each(e.errors, function (key, value) {
            if ('errors' in value) {
                $.each(value.errors, function () {
                    message += this + "\n";
                });
            }
        });
        alert(message);
    }
 }


</script>

我已将我的视图更新到现在并且现在绑定选择列表问题现在它在网格内显示文本框而不是下拉列表..这是我更新的索引视图:

这是我的索引视图:

 @model IEnumerable < PPMS.Model.ViewModels.AccountingViewModel >
 < br/ >

< div class="k-grid" >

 @(Html.Kendo().Grid(Model)
.Name("grid")
.Columns(columns =>
       {
             columns.Bound(p => p.Code);
             columns.Bound(p => p.Description).Width(150);
              columns.ForeignKey(p => p.MajorCategoryId, (System.Collections.IEnumerable)ViewData["majorCategories"], "Value", "Text")
                           .Title("MajorCategory").Width(150);

         columns.Command(command => command.Destroy()).Width(110);
                   })
      .ToolBar(toolBar =>
                   {
                       toolBar.Save();
                       toolBar.Create();
                   })
      .Editable(editable => editable.Mode(GridEditMode.InCell))
      .Filterable()
      .Groupable()
      .Pageable()     
      .Scrollable()
      .HtmlAttributes(new { style = "height:430px;" })    
      .DataSource(dataSource => dataSource
                    .Ajax()
                    .Batch(true)
                    .PageSize(20)
                    .ServerOperation(false)
                    .Events(events => events.Error("errorHandler"))
                     .Model(model =>
                                    {
                                        model.Id(p => p.Id);
                                        model.Field(p => p.Id).Editable(false);
                                   model.Field(p => p.MajorCategoryId).DefaultValue(1);       
                                               })

                       .Create(create => create.Action("Create", "Accounting"))
                       .Read(read => read.Action("Index", "Accounting"))  
                       .Update(update => update.Action("Edit", "Accounting")) 
                       .Destroy(destroy => destroy.Action("Delete", "Accounting"))
       )
      )
</div>
<br/>


<script type="text/javascript">


function errorHandler(e) {
    if (e.errors) {
        var message = "Errors:\n";
        $.each(e.errors, function (key, value) {
            if ('errors' in value) {
                $.each(value.errors, function () {
                    message += this + "\n";
                });
            }
        });
        alert(message);
    }
 }


</script>

1 个答案:

答案 0 :(得分:0)

外国下拉列表应包含具有“值”和“文本”属性的项目?

像:

  

var categories = [{                       “价值”:1,                       “文字”:“饮料”                   },{                       “价值”:2,                       “文字”:“调味品”                   }                   }];