Search code examples
asp.net-mvc-3html.listboxfor

MVC 3 - Getting all selected values from a listbox on postback


I have an MVC3 project that has a listbox (listboxfor) like this:

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

When testing, I am able to select multiple entries in the list and even added a event handler to popup the comma-limited list of the selected values when the they are selected. However, when I post the form back to the controller, I only get the first selected value returned. For example, if I select value 1,2,3,4 - my JQuery event handler will popup "1,2,3,4". No problem - but on post back, the bound object referenced in the controller only shows a "1".

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

Any help would be greatly appreciated.

EDIT:

Here's the EventReport data model/class:

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
    }
}

Thanks!


Solution

  • Try to change the property type to string[] or int[]. I think the default model binder expects an array, and since in your case it was not an array it just populated it with the first selected value. If you must stick to this model class, you might have to tweak the binding (e.g. with a customized model binder).