0

I have a form that contains a multiple selectlist and I also use bootstrap selectpicker with this.

code:

model:

    [Display(Name = "SystemTyp")]
    [Required(ErrorMessage = "Vänligen välj typ")]
    public List<SelectListItem> SystemTypes { get; set; }

view:

    <div class="form-group">
        @Html.Label("SystemTyp", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.DropDownList("SystemTypes",
           RegistrationHandlers.GetSystemtypes()
           ,
           new { @class = "form-control", @multiple = "multiple", @title = "---  Välj Systemtyp  ---" })
            @Html.ValidationMessageFor(model => model.SystemTypes, "", new { @class = "text-danger" })
        </div>
    </div>

When posting:

enter image description here

Every time I post the list is empty. The list name matches the model property name.

What am I missing?

I have another list that is a single select so the selected value is a simple string and this works fine but the above is giving me a headache.

teo van kot
  • 12,350
  • 10
  • 38
  • 70
ThunD3eR
  • 3,216
  • 5
  • 50
  • 94
  • You `SelectList` property is named `SystemTypes` and the view code you have shown does not use that anywhere. All you have shown is a `DropDownList()` method that binds to a property named `UserRole` –  Sep 01 '16 at 11:41
  • Copied the wrong dropdownlist, sorry, check edits – ThunD3eR Sep 01 '16 at 11:44
  • 1
    `SystemTypes` is your `SelectList`. You cannot bind to a `SelectList`. Your property needs to be (say) `IEnumerable SystemTypes` (a ` –  Sep 01 '16 at 11:47
  • 3
    And do not use `DropDownList()` - use `ListBoxFor(m => m.SystemTypes, RegistrationHandlers.GetSystemtypes(), new { ... })` which is the correct way to generate a ` –  Sep 01 '16 at 11:48
  • @Stephen Muecke as of late you are like a guardian angel. thanks m8 – ThunD3eR Sep 01 '16 at 11:53
  • @StephenMuecke **is** an angel... (no-homo...) – Geoff James Sep 01 '16 at 11:54
  • hahahhahaha I like the (no-homo) edit. Developer skill level = precise – ThunD3eR Sep 01 '16 at 12:02

1 Answers1

2

You should inderstand that DropDownList helper creates select tag with name="SystemTypes" attribute in html markup.

On POST in passes selected value with UserRole name.

And you don't need whoule list on POST you need only selected value so create SystemTypeId property in your ViewModel and change your helper to this:

 @Html.DropDownList("SystemTypeId", <-- note this
           RegistrationHandlers.GetSystemtypes()
           ,
           new { @class = "form-control", @multiple = "multiple", @title = "---  Välj Systemtyp  ---" })

Then you will get selected value in your binded model.

Don't try to get whoulde list back - you don't need it.

If you need to select multiple you should use ListBox helper:

@Html.ListBox("SystemTypeIds", <-- note this
               RegistrationHandlers.GetSystemtypes()
               ,
               new { @class = "form-control", @title = "---  Välj Systemtyp  ---" })

SystemTypeIds property should be Array or IEnumerable<int> or IList<int> to bind correctry. (ofcource it could be not only int but string, bool etc..)

If you searching for best way to achive that i suggest you to use Strongly typed helper - ListBoxFor:

@Html.ListBoxFor(x => x.SystemTypeIds
               ,RegistrationHandlers.GetSystemtypes()
               ,new { @class = "form-control", @title = "---  Välj Systemtyp  ---" })
teo van kot
  • 12,350
  • 10
  • 38
  • 70