【问题标题】:dropdownlist in kendo grid not working剑道网格中的下拉列表不起作用
【发布时间】:2014-08-18 14:38:29
【问题描述】:

我尝试使用 incell 编辑和使用 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();    
    }

尝试渲染网格时出现此错误: 您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。

我错过了什么?

【问题讨论】:

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


    【解决方案1】:

    首先尝试使用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() 或脚本标签。您将通过文件名引用它。然后,您可以在项目的多个页面上使用该模板。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多