2011-09-06 107 views
0

我有了一個列表框(listboxfor)像這樣的MVC3項目:MVC 3 - 獲取所有選擇值從列表框上回發

@Html.ListBoxFor(m => m.cat_fam_codes, new SelectList(BaanWrapper.GetAllCategoryFamilies(), "Code", "Description"), new { @size = "10" }) 

測試時,我能夠在列表中選擇多個條目甚至添加了一個事件處理程序,以便在選擇它們時彈出所選值的逗號限制列表。但是,當我將表單發回控制器時,我只能得到返回的第一個選定值。例如,如果我選擇值1,2,3,4 - 我的JQuery事件處理程序將彈出「1,2,3,4」。沒問題 - 但在回發時,控制器中引用的綁定對象只顯示「1」。

[HttpPost] 
public ActionResult Create(EventReport eventReport) 
{ 
    return View(eventReport); 
} 

任何幫助將不勝感激。

編輯:

這裏的EventReport數據模型/類:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 

namespace EWS.Models 
{ 
    [MetadataType(typeof(EventReportMetaData))] 
    public partial class EventReport 
    { 
     private static readonly EWSDataContext db = new EWSDataContext(); 

     #region Nested type: MetaData 

     private sealed class EventReportMetaData 
     { 
      [DisplayName("Alert Email Body")] 
      [Required(ErrorMessage = "Required")] 
      public string alert_body { get; set; } 

      [DisplayName("Alert Email Subject")] 
      [Required(ErrorMessage = "Required")] 
      public string alert_subject { get; set; } 

      [DisplayName("Categories")] 
      public string cat_codes { get; set; } 

      [DisplayName("Category Families")] 
      public string cat_fam_codes { get; set; } 

      [DisplayName("CCR Codes")] 
      public string ccr_codes { get; set; } 

      [Required(ErrorMessage = "Required")] 
      public DateTime create_dtm { get; set; } 

      [Required(ErrorMessage = "Required")] 
      public int created_by { get; set; } 

      [DisplayName("Email List")] 
      [Required(ErrorMessage = "Required")] 
      [IsInteger(ErrorMessage = "Invalid Email List")] 
      [IsGreaterThanZero(ErrorMessage = "Required")] 
      public int email_list_id { get; set; } 

      [DisplayName("Begin Date")] 
      public DateTime begin_dtm { get; set; } 

      [DisplayName("End Date")] 
      public DateTime end_dtm { get; set; } 

      [DisplayName("Event Threshold")] 
      [Required(ErrorMessage = "Required")] 
      [IsInteger(ErrorMessage = "Invalid Threshold")] 
      public int event_count { get; set; } 

      [DisplayName("Disabled")] 
      public bool is_disabled { get; set; } 

      [DisplayName("Notify Over Threshold")] 
      [Required(ErrorMessage = "Required")] 
      public bool notify_over_events { get; set; } 

      [DisplayName("Notify Under Threshold")] 
      [Required(ErrorMessage = "Required")] 
      public bool notify_under_events { get; set; } 

      [Required(ErrorMessage = "Required")] 
      public int report_id { get; set; } 

      [DisplayName("Report Title")] 
      [Required(ErrorMessage = "Required")] 
      public string report_title { get; set; } 

      [DisplayName("Reset By")] 
      public int reset_by { get; set; } 

      [DisplayName("Reset Date")] 
      public DateTime reset_dtm { get; set; } 

      [DisplayName("SKUs")] 
      public string skus { get; set; } 

      [DisplayName("Source System")] 
      [Required(ErrorMessage = "Required")] 
      public string source_system { get; set; } 

      [DisplayName("Sub-Categories")] 
      public string sub_cat_codes { get; set; } 

      [DisplayName("# of Time Units")] 
      [Required(ErrorMessage = "Required")] 
      public int window_dt_part_count { get; set; } 

      [DisplayName("Unit of Time")] 
      [Required(ErrorMessage = "Required")] 
      public string window_dt_part { get; set; } 
     } 

     #endregion 
    } 
} 

謝謝!

+0

你的eventReport模型是什麼樣的? –

回答

1

嘗試將屬性類型更改爲string []或int []。我認爲默認的模型綁定器需要一個數組,因爲在你的情況下,它不是一個數組,它只是用第一個選定的值填充它。如果您必須堅持這個模型類,您可能需要調整綁定(例如,使用自定義模型綁定器)。

0

好露營這裏是我做了什麼......我將使用cat_code(來自我的數據庫模型)的一個下拉列表作爲示例。

修改我的模型類,像這樣:

public IList<string> SelectedCategoryCodes 
{ 
    get 
    { 
     if (string.IsNullOrWhiteSpace(cat_codes)) 
      return new List<string>(); 

     return cat_codes.Split(',').ToList(); 
    } 
    set { cat_codes = string.Join(",", value.ToArray()); } 
} 

進一步上下來,我不得不

private sealed class EventReportMetaData 
{ 
    .... 
    [DisplayName("Categories")] 
    public string cat_codes { get; set; } 
    .... 
} 

自從我保存了多個選擇的值作爲數據庫中的一個逗號分隔的字符串,我加入這個到MVC模型,以便存儲在該字段中的數據(cat_codes)可以在視圖中讀取和寫入。

然後在我看來,我改變了ListBoxFor聲明是這樣的:

@Html.ListBoxFor(m => m.SelectedCategoryCodes, new SelectList(BaanWrapper.GetAllCategories(), "Code", "Description"), new {@size = "10"}) 

就是這樣。由於我的數據庫模型已經有了一個cat_codes字段,所有東西都已經到位了。

希望這可以幫助別人。

相關問題