2014-02-05 46 views
2

我KendoUI多選只返回第一個選定的值,如果沒有被改變。KendoUI MVC多重選擇 - 在某些情況下,只有一個返回值

當我加載的ViewPage,將MultiSelect與數據和多個選擇正確填充。當我現在繼續保存數據而沒有更改任何內容(!)時,傳送到我的控制器的模型(大部分時間,沒有可辨別的模式)只包含合適字段中的第一個選定項目。如果我添加新的選擇,則只有先前選擇的項目中的第一個與新選擇的項目一起返回。

如果我使用.AutoBind(假的),然後單擊一次到現場,然後外面再不必去/選擇任何內容,轉移到我的控制器模型包含對儲蓄的全部內容。

對於同一ViewPage中的多個MultiSelect(在不同的數據上),在一個MultiSelect上使用.AutoBind(false)就足夠了(隨後的點擊如上),以便所有MultiSelects突然返回所選的全部範圍值。

誰能請解釋這種奇怪的行爲對我來說,甚至可能有一個修復?在視圖

兩種不同的實現方式中,其中兩個顯示相同的行爲(GetDepotList()返回類似的內容到計算機[「DepotList」]):

@model List<LoadingUnitViewModel> 

[...] 

//Version 1 
@(Html.Kendo().MultiSelectFor(m => m[i].FK_Depots) 
    .Name("[" + i.ToString() + "]." + "FK_Depots") 
    .Placeholder(Localize((string)ViewData["ControllerName"], "ChooseDepot")) 
    .DataValueField("Value") 
    .Value(Model[i].FK_Depots) 
    .DataTextField("Text") 
    .DataSource(source => 
    { 
     source.Read(read => 
     { 
      read.Action("GetDepotList", (string)ViewData["ControllerName"]); 
     }) 
     .ServerFiltering(true); 
    }) 
    .HtmlAttributes(new { id = "_" + i + "__FK_Depots"}) 
) 

//Version 2 
@(Html.Kendo().MultiSelectFor(m => m[i].FK_Depots) 
    .Name("["+i.ToString()+"]."+"FK_Depots") 
    .Placeholder(Localize((string)ViewData["ControllerName"], "ChooseDepot")) 
    .BindTo(new MultiSelectList((System.Collections.IEnumerable)ViewData["DepotList"], "Id", "Name", Model[i].FK_Depots)) 
) 

編輯: 按照要求:所述的相關部分在LoadingUnitViewModel:

public class LoadingUnitViewModel 
{ 
    [Editable(false)] 
    [Display(Name = "Id")] 
    public int Id { get; set; } 

    [Required] 
    [Display(Name = "Name")] 
    public string Name { get; set; } 

    [Display(Name = "FK_Depots")] 
    public List<string> FK_Depots { get; set; } 

} 
+0

你的'LoadingUnitViewModel'看起來像什麼? –

+0

已被編輯爲原始文章。 – MilConDoin

+0

你有使用AJAX綁定的原因嗎?我有一個想法,但不需要這樣綁定。 –

回答

3

我們碰到的有劍道多選一些奇怪的問題,而不是綁定到MVC模式,我會告訴你我們做了什麼。問題的一部分看起來像是你的多重選擇的名字。我不知道爲什麼你需要像你一樣產生多個名字。如果你給它一個單一的名字屬性,你可以得到所有的值。這是我的multiselect看起來像。注意名稱就是「廣告」

@(Html.Kendo().MultiSelect() 
      .Name("Advertisers") 
      .BindTo(Model.Advertisers) 
      .HtmlAttributes(new { style="width:445px;" }) 
      .DataTextField("Text") 
      .Filter("startswith") 
      .MinLength(5) 
      .MaxSelectedItems(3) 
      .DataValueField("Value") 
      .AutoBind(false)     
      .Placeholder("Type the first five characters of the advertiser name...")     
      .Value(Model.Advertisers) 
      .HtmlAttributes(new {style = "width:415px;"})     
      .Events(e => e.Select("advertiserSelected"))     
      .DataSource(source => 
      { 
       source.Read(read => 
       { 
        read.Action("GetAccounts", "CrmIntegration"); 
       }) 
       .ServerFiltering(true); 
      }) 
     )  

在表單提交您的控制器內,該值是不會的模式,所以我們反而把他們這個樣子。

  string[] advertisers = Request.Form["Advertisers"].ToString().Split(','); 
      foreach (var s in advertisers) 
      { 
       // access to the values in multiselect 
      } 

編輯
應當指出的是,default model binder會看您是否使用MultiSelectFor或不值在.Name("Property")命名屬性綁定不管。發佈這個答案後,我意識到這一點。

@(Html.Kendo().MultiSelectFor(x => x.AdvId) 
.Name("Advertisers") 
//will bind to Advertisers, not AdvId on post 
+0

我試過了你的Request.Form,但它總是和我的模型有相同的結果。如果模型在列表中有多個條目,則該請求還會返回多個條目。如果模型只有一個條目,請求也只返回一個條目。名稱的格式爲[i] .name,因爲在某些情況下,視圖可以包含多個模型(這就是@model List 的原因)。 – MilConDoin

相關問題