簡體   English   中英

劍道網格中的dropdownlist不起作用

[英]dropdownlist in kendo grid not working

我正在嘗試使用單元格編輯和http://demos.telerik.com/aspnet-mvc/grid/editing-custom的使用,但是找不到我的部分觀點,即應該從下拉菜單中進行渲染。

局部視圖(Testview.cshtml)

   @using Kendo.Mvc.UI

 @(Html.Kendo().DropDownList()
        .Name("ResourceType") // Name of the widget should be the same as the name of the property
.DataValueField("Id") // The value of the dropdown is taken from the EmployeeID property
.DataTextField("Name") // The text of the items is taken from the EmployeeName property
    .BindTo((System.Collections.IEnumerable)ViewData["defaultResourceType"]) // A list     of all employees which is populated in the controller

這是我的網格:

  @(Html.Kendo().Grid<RMS.Admin.ViewModel>()
  .Name("ResourceGrid")
  .Columns(columns =>
  {
      columns.Bound(c => c.ResourceId).Hidden();
      columns.Bound(c => c.ResourceName);
      columns.Bound(c => c.Descritption);
      columns.Bound(c => c.ResourceType.Name).ClientTemplate("#=ResourceType.Name#");
      columns.Bound(c => c.Approved);
      columns.Bound(c => c.IsEnabled);
      columns.Command(command =>
      {
          command.Edit();
          command.Destroy();
      }).Width(172).Title("Edit/Delete");
  })
  .ToolBar(toolbar => toolbar.Create())
  .Editable(editable => editable.Mode(GridEditMode.InCell))
  .Scrollable()
  .Sortable()
  .Pageable(pageable => pageable
      .Refresh(true)
      .PageSizes(true)
      .ButtonCount(5))
  .DataSource(dataSource => dataSource
      .Ajax()
      .Model(model =>
      {
          model.Id(s => s.ResourceId);
          model.Field(s => s.ResourceType).DefaultValue(ViewData["defaultResourceType"] as RMS.Admin.ViewModel.ResourceTypeId);
      })
      .Create(update => update.Action("CreateResource", "Home"))
            .Read(read => read.Action("ReadResource", "Home"))
            .Update(update => update.Action("SaveSystem", "Home"))
            .Destroy(destroy => destroy.Action("RemoveSystem", "Home"))
    )

這是我的模型的一部分:

  public string ResourceUserId { get; set; }
    [UIHint("Testview")]
    public ResourceTypeId ResourceType { get; set; }

這是在我綁定數據的控制器中:

    private void GetResourceTypeId()
    {
        //string [] list = new string[]{"Image", "Document", "Other"};
        IList<ViewModel.ResourceTypeId> list = new List<ViewModel.ResourceTypeId>();

        var a = new ViewModel.ResourceTypeId
        {
            Name = "Image",
            Id = 1
        };
        var b = new ViewModel.ResourceTypeId
        {
            Name = "Document",
            Id = 2
        };
        var c = new ViewModel.ResourceTypeId
        {
            Name = "Other",
            Id = 3
        };

        list.Add(a);
        list.Add(b);
        list.Add(c);
        ViewData["defaultResourceType"] = list.First();    
    }

在嘗試渲染網格時出現此錯誤:您正在尋找的資源(或其依賴項之一)可能已被刪除,名稱更改或暫時不可用。 請查看以下網址,並確保其拼寫正確。

我想念什么?

首先,您嘗試使用ViewData["defaultResourceType"] = list.First();僅綁定到一個項目ViewData["defaultResourceType"] = list.First(); 相反,您應該綁定到整個列表,然后使用.Value("1")設置默認選項,以便默認情況下具有“圖像”。

@(Html.Kendo().DropDownList()
  .Name("ResourceType") 
  .DataValueField("Id")
  .DataTextField("Name")
  .BindTo((System.Collections.IEnumerable)ViewData["ResourceTypeList"])
  .Value(ViewData["DefaultResourceType"])
);

同樣對於MVC中列的模板,您可能只想使用EditorTemplateName進行設置

columns.Bound(e => e.myColumn).EditorTemplateName("dropdownTemplate")

然后在頁面上的其他地方定義要使用的模板。

<script id="dropdownTemplate" type="text/x-kendo-template">
  @(Html.Kendo().DropDownList()
    .Name("myDropDown")
    .....
    .ToClientTemplate()
  )
</script>


如果您願意,也可以在/ Views / Shared /文件夾中將其聲明為獨立模板。 您不必添加.ToClientTemplate()或腳本標簽。 您可以通過文件名來引用它。 然后,您可以在項目的多個頁面上使用模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM