2013-07-22 108 views
2

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

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; } 
    } 

VIEW

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

控制器

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

回答

1

谢谢,我终于想通了以L试验和失败。 基本上,调用FilterChild服务器方法时,代码传递的是parentID的空值。所有的代码都是这样的,我只是做了一些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

感谢您对这个响应的启发。

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

它被命名为CascadingFromField

0

你可以在孩子下拉使用.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为空,你应该设置你的下拉菜单来像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是这样的,在那里你可以默认值到那个“无”的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(); 
} 
相关问题