2013-07-24 88 views
3

我有以下三個KendoUI下拉列表框無法讀取KendoUI下拉列表值

@(Html.Kendo().DropDownList() 
    .HtmlAttributes(new { style = "width:120px;height:20px;font-size:12px;" }) 
    .OptionLabel("Make (any)") 
    .Name("Make") 
    .DataTextField("Name") 
    .DataValueField("Id") 
    .DataSource(source => 
    { 
      source.Read(read => 
      { 
       read.Action("UpdateFilters", "Home"); 
      }) 
      .ServerFiltering(true); 
    }) 
    .Events(e => e.Change("UpdateFilterParameters")) 
    .SelectedIndex(0) //Select first item. 
    ) 

    @(Html.Kendo().DropDownList()  
      .Name("Model") 
      .HtmlAttributes(new { style = "width:120px;height:20px;font-size:12px;'" }) 
      .OptionLabel("Model (any)") 
      .DataTextField("Name") 
      .DataValueField("Id") 
      .DataSource(source => { 
       source.Read(read => 
       { 
        read.Action("UpdateFilters", "Home"); 
       }) 
       .ServerFiltering(true); 
      }) 
      .Enable(true) 
      .AutoBind(true) 
      .Events(e => e.Change("UpdateFilterParameters")) 


    ) 

    @(Html.Kendo().DropDownList() 
      .Name("Fuel") 
      .HtmlAttributes(new { style = "width:120px;height:20px;font-size:12px;" }) 
      .OptionLabel("Fuel type (any)") 
      .DataTextField("Name") 
      .DataValueField("Id") 
      .DataSource(source => 
      { 
       source.Read(read => 
       { 
        read.Action("UpdateFilters", "Home"); 
       }) 
       .ServerFiltering(true); 
      }) 
      .Enable(true) 
      .AutoBind(true) 
      .Events(e => e.Change("UpdateFilterParameters")) 


    ) 

這是我的MVC控制器

public JsonResult UpdateFilters(string FilterInfo) 
    { 
     FilterParameter oFilterInfo = new FilterParameter(); 

     // Json object for filtering 
     // {'Procedure':'GetMakes','Id':[1,2,3]} 
     try 
     { 
      oFilterInfo = JsonConvert.DeserializeObject<FilterParameter>(FilterInfo); 
      return Json(UpdateFilterParameter.GetUpdatedFilters(oFilterInfo), JsonRequestBehavior.AllowGet); 
     } 
     catch 
     { 
      return new JsonResult(); 
     } 

    } 

這是我班舉行的JSON數據

public class FilterParameter 
{ 
    public string Procedure { get; set; } 
    public int?[] Id; 

    public FilterParameter() 
    { 
    } 
} 

我遇到的問題是在調用JavaScript函數UpdateFilterParameters時,我只是getti ng列表框的值,但不是全部。這是功能;

function UpdateFilterParameters() { 

     var iMake = $("#Make").val(); 
     var iModel = $("#Model").val(); 
     var iFuel = $("#Fuel").val(); 

     $('#Make').data("kendoDropDownList").dataSource.read(
      { FilterInfo: "{'Procedure':'GetMakes','Id':[" + iMake + "," + iModel + "," + iFuel + "]}" }); 

     $('#Model').data("kendoDropDownList").dataSource.read(
      { FilterInfo: "{'Procedure':'GetModels','Id':[" + iMake + "," + iModel + "," + iFuel + "]}" }); 

     $('#Fuel').data("kendoDropDownList").dataSource.read(
      { FilterInfo: "{'Procedure':'GetFuelTypes','Id':[" + iMake + "," + iModel + "," + iFuel + "]}" }); 

    }; 

任何想法爲什麼會發生這種情況?

+0

是否需要更多信息來幫助解決我的問題? – Tommassiov

回答

3

要解決這個問題,我不得不將函數UpdateFilterParameters更改爲如下所示,以顯示Id值或傳遞NULL。當解序列化Json時,我的代碼出現故障

function UpdateFilterParameters() { 

     var iMake = $("#Make").val(); 
     var iModel = $("#Model").val(); 
     var iFuel = $("#Fuel").val(); 

     var ids = (iMake || "null") + "," + (iModel || "null") + "," + (iFuel || "null"); 

     $('#Make').data("kendoDropDownList").dataSource.read(
      { FilterInfo: "{'Procedure':'GetMakes','Id':[" + ids + "]}" }); 

     $('#Model').data("kendoDropDownList").dataSource.read(
      { FilterInfo: "{'Procedure':'GetModels','Id':[" + ids + "]}" }); 

     $('#Fuel').data("kendoDropDownList").dataSource.read(
      { FilterInfo: "{'Procedure':'GetFuelTypes','Id':[" + ids + "]}" }); 

    }; 
+0

嘿我試圖實現你有什麼,因爲我doens't工作@'http:// stackoverflow.com /問題/ 18410895 /如何閱讀 - kendoui - 級聯 - 下拉列表從控制器',但在你的例如我無法獲得'JsonConvert.'或'UpdateFilterParameter.'它們都是'Err:'不存在於當前contedxt中。你能幫我嗎? –

+0

您是否添加了對使用Newsoft.Json的引用? – Tommassiov

+0

當然有,你能想到的其他東西?你也認爲我有一個很好的理由,爲什麼我不能從我的formCollection值和控制器級別的DDl的Name/id做'string filter3 = values [「DDLID」]? –