Kendo ui 不使用CascadeFrom的剑道UI级联下拉列表

Kendo ui 不使用CascadeFrom的剑道UI级联下拉列表,kendo-ui,kendo-asp.net-mvc,Kendo Ui,Kendo Asp.net Mvc,有没有一种方法可以在不使用CascadeFrom的情况下创建两个级联下拉列表(即手动触发事件)?我不想使用CascadeFrom的原因是,由于两个模型中的属性名称相同,我的父和子下拉列表都将DataValueField设置为DataValueField(“ID”),如下所示 型号: class ParentDropdownModel { public int ID { get; set; } public string Name { get; set; }

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

型号

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

    class ChildDropdownModel
    {
     public int ID { get; set; }
    public string Name { get; set; }
    }
public ActionResult FilterChild([DataSourceRequest] DataSourceRequest request, string parentID)
{
    // Here is the Problem: parentID is null at run-time
    return Json(dummyData, JsonRequestBehavior.AllowGet);
}
查看

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

谢谢,经过多次尝试和失败,我终于找到了答案。 基本上,当调用FilterChild服务器方法时,代码传递parentID的null值。所有的代码都是原样,我只是在JavaScript代码中做了一些更改,所以现在它调用服务器端方法并传递parentID参数的实际值。
以下是视图的部分代码

这是可行的,但请告诉我是否有比这更好的方法。我愿意学习

查看:

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.
此解决方案的灵感来自此帖子
谢谢您的回复

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


它的名称为。

如果您的子对象中有一个外键,并且该外键与其父对象相关,则可以在子对象下拉列表中使用
.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; }
}
FilterChild如下所示,如果返回时该父项没有子项,则可以将该值默认为“None”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();
}
public JsonResult FilterChild(字符串parentID)
{
int parentId=int.Parse(parentId);
列表数据=(列表)GetChildrenForParent(父ID);
if(data.Count()==0){
Child nullChild=新子项();
nullChild.Id=1;
nullChild.ParentId=ParentId;
nullChild.Name=“无”;
data.Add(nullChild);
}
返回Json(数据,JsonRequestBehavior.AllowGet());
}
公共IList GetChildrenForParent(int parentId)
{
返回datacontext.Children.Where(c=>c.ParentId==ParentId)
.OrderBy(c=>c.Name)
.ToList();
}
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();
}