Kendo UI Cascading DropDownList,不使用CascadeFrom

时间:2013-07-22 21:24:43

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

有没有办法在不使用CascadeFrom(即手动触发事件)的情况下有两个级联下拉列表?我不想使用CascadeFrom的原因是因为我的父级和子级下拉列表DataValueField都设置为DataValueField(“ID”),因为两个模型中的属性名称相同,如下所示。

MODEL

 class ParentDropdownModel
    {
     public int ID { get; set; }
     public string Name { get; set; }
    }

    class ChildDropdownModel
    {
     public int ID { get; set; }
    public string Name { get; set; }
    }

查看

@(Html.Kendo().DropDownList()
.AutoBind(true)
.Name("ddlParent")        
.DataTextField("Name")
.DataValueField("ID")
.OptionLabel("Select a parent...")
.DataSource(ds => ds.Read(read => read.Action("ReadParent", "Home")))
.Events(e => e.Change("OnParentChanged"))
)

@(Html.Kendo().DropDownList()
.AutoBind(false)
.Name("ddlChild")
.DataSource(ds => ds.Read(read => read.Action("FilterChild", "Home").Data("filterChild")))
.DataTextField("Name")
.DataValueField("ID")
.OptionLabel("Select a child...")
)   

<script type="text/javascript">          
    function OnParentChanged(e)
    {            
        var child = $('#ddlChild').data("kendoDropDownList");           
        child.dataSource.read(filterChild());            
    }
    function filterChild()
    {
        var myid = $("#ddlParent").val();            
        return
        {                
            parentID: $("#ddlParent").val()
        };
    }    
</script>

CONTROLLER

public ActionResult FilterChild([DataSourceRequest] DataSourceRequest request, string parentID)
{
    // Here is the Problem: parentID is null at run-time
    return Json(dummyData, JsonRequestBehavior.AllowGet);
}

3 个答案:

答案 0 :(得分:2)

谢谢,我终于通过大量的试验和失败来解决这个问题。 基本上,代码在调用FilterChild服务器方法时为parentID传递null值。所有代码都是原样的,我只是在JavaScript代码中进行了一些更改,所以它现在调用服务器端方法并传递parentID参数的实际值。
以下是View的部分代码。

这有效,但让我知道是否有比这更好的方法。我愿意学习。

查看:

function OnParentChanged(e)
    {        
         var child = $('#ddlChild').data("kendoDropDownList");     
        child.enable(true);
        var myid = $("#ddlParent").val();
        child.dataSource.read({ parentID: myid });
    }

 //IMPORTANT: NO NEED TO CALL filterChild() FUNCTION, 
 //   Just pass JSON key value pair AS ABOVE.

这个解决方案的灵感来自这篇文章 Combobox Cascading need more specific cascadeFrom option

答案 1 :(得分:1)

感谢您的回复。

我认为这类示例有一个新的配置字段。

它名为CascadingFromField

答案 2 :(得分:1)

如果孩子的外键与其父母相关,则可以在子下拉列表中使用.CascadeFrom("ddlParent")

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

class Parent
{
   public (Parent) 
   {
        Children = new HashSet<Child>();
   }

   public int ID { get; set; }
   public string Name { get; set; }

   public virtual ICollection<Child> Children { get; set;} // navigation property for MVC
}

class Child
{
   public int ID { get; set; }
   public string Name { get; set; }

   [Column("ParentID")]
   public int ParentId { get; set; } // this is the 'ID' value of the Parent object

   [ForeignKey("ParentId")]
   public virtual Child Child { get; set; } // navigation property for MVC
}

class MainModel
{
    [DisplayName("Child")]
    [ColumnName("ChildID")]
    public int ChildId { get; set; }

    [ForeignKey("ChildID")]
    public virtual Child Child { get; set; }
}

这通常是如何设置数据库关系/导航属性的。然后,您不需要.Events(e => e.Change("OnParentChanged"))因为它会在更改父级时自动更新子级。您确实需要确保传入FilterChild的ID返回基于父ID的过滤的子项,但是在控制器中的该函数中。为避免该ID为null,您应将下拉列表设置为初始值,如&#34;无&#34;在ID#1上,而不是使用OptionLabel:

@model MainModel

<table><tr><td>Parent:</td><td>
@(Html.Kendo().DropDownListFor(model => model.Child.ParentId)
    .AutoBind(true)
    .Name("ddlParent")        
    .DataTextField("Name")
    .DataValueField("ID")
    .DataSource(ds => ds.Read(read => read.Action("ReadParent", "Home")))
    .Value(Model.Child != null ? Model.Child.ParentId.ToString() : "1")
)
</td></tr><tr><td>Child:</td><td>
@(Html.Kendo().DropDownListFor(model => model.ChildId)
    .AutoBind(false)
    .Name("ddlChild")
    .DataSource(ds => ds.Read(read => read.Action("FilterChild", "Home").Data("filterChild")))
    .DataTextField("Name")
    .DataValueField("ID")
    .CascadeFrom("ddlParent")
    .Value(Model.ChildId != 0 ? Model.ChildId.ToString() : "1")
)   
</td></tr></table>

FilterChild看起来像这样,你可以将值默认为&#34;无&#34; ID,如果它没有为该父母的孩子回来:

public JsonResult FilterChild(string parentID)
{
    int parentId = int.Parse(parentID);
    List<Child> data = (List<Child>)GetChildrenForParent(parentId);
    if (data.Count() == 0) {
        Child nullChild = new Child();
        nullChild.Id = 1;
        nullChild.ParentId = parentId;
        nullChild.Name = "None";
        data.Add(nullChild);
    }
    return Json(data, JsonRequestBehavior.AllowGet());
}

public IList<Child> GetChildrenForParent(int parentId)
{
    return datacontext.Children.Where(c => c.ParentId == parentId)
            .OrderBy(c => c.Name)
            .ToList();
}