2013-06-19 99 views
0

嗨我用下拉列表使用Kendo網格,我按照演示http://demos.kendoui.com/web/grid/foreignkeycolumn.html 但我收到此錯誤「DataBinding:'System.Web.Mvc.SelectListItem'不包含名稱爲' ID'」。Kendo Grid with DropdownList

誰能幫助我使我的工作沒有錯誤?

這裏是我的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> 

我我的觀點已經更新到和現在結合選擇列表的現在的問題網格而不是下拉列表中的顯示文本。這裏是我的更新d索引視圖:

這裏是我的索引視圖:

@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> 

回答

0

外國下拉列表中應包含具有「價值」和「文本」屬性的項目?

喜歡:

變種類別= [{ 「值」:1, 「文本」: 「飲料」 },{ 「值」:2, 「文本」:「調味品「 } }];