2013-07-22 207 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(); 
}