Model.State is invalid due to MultiSelectList not being able to Convert IEnumerable<SelectListItem> to string

Roman

So I have 2 ListBox controls one is filled with options the other one is empty. The point is when I submit the form I can see which values they have selected from the second ListBox. But when is posts the modelstate is invalid with this error for one of these values:

{"The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types."} for that

html:

@Html.ListBoxFor( x => x.RolesAvailable, new MultiSelectList(Model.RolesAvailable, "Value", "Text", Model.RolesAvailable),
                 new {Multiple = "multiple"}
            )  

@Html.ListBoxFor( x => x.RolesSelected, new MultiSelectList(Model.RolesSelected, "Value", "Text", Model.RolesSelected),
                                       new { Multiple = "multiple" }
            ) 

model:

    public IEnumerable<SelectListItem> RolesAvailable { get; set; }

    public IEnumerable<SelectListItem> RolesSelected { get; set; }

controller (avm is my viewmodel):

            List<SelectListItem> sRoles = new List<SelectListItem>();
            foreach (SelectListItem SI in avm.RoleListItems)
            {
                sRoles.Add(SI);
            }

            avm.RolesAvailable = sRoles;

            List<SelectListItem> items = new List<SelectListItem>();
            avm.RolesSelected = items;
Réda Mattar

When you write @Html.ListBoxFor( x => x.RolesSelected ..., it means that RolesSelected contains selected values, those selected values are a collection of the same type as your Value property. If your options' values are integers, the collection used for @Html.ListBoxFor( x => x.MySelectedValues ... must be an IEnumerable<int>.

You should make two separate collections in your view model : an IEnumerable<SelectListItem> used to build your ListBox, and an IEnumerable<*MyValueType*> used to get your selected values, if the Value property is a value type or a string.

Also, the 4th parameter used in your new MultiSelectList is also a collection of selected values :

@Html.ListBoxFor(x => x.SelectedRoles, 
                 new MultiSelectList(Model.RolesAvailable, "Value", "Text", Model.SelectedRoles),
                 new {Multiple = "multiple"})  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>

From Dev

Convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>

From Dev

Convert List to IEnumerable<SelectListItem>

From Dev

Making a model being able to convert itself to JSON

From Dev

Making a model being able to convert itself to JSON

From Dev

Not being able to convert list or string into dict

From Dev

Scaffolded model/view: The ViewData item that has the key 'COLUMN' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'

From Dev

"There is no ViewData item of type 'IEnumerable<SelectListItem>'" error with custom model and list

From Dev

Convert IEnumerable<string> into IEnumerable<FileInfo>

From Dev

Not able to convert a string to XML

From Dev

Convert IEnumerable<string> to ICollection<string>

From Dev

How to convert IEnumerable<char> to IEnumerable<string>?

From Dev

CodeKit unable to compile due to not being able to create a cache folder

From Dev

CodeKit unable to compile due to not being able to create a cache folder

From Dev

SelectListItem Text not being displayed

From Dev

how to bind selectlistitem value in dropdownlist to string property in model

From Dev

Not able to convert string to decimal in mvc

From Dev

Not able to convert string into a char array

From Dev

Not able to convert the string to date on Android

From Dev

How to add item to IEnumerable SelectListItem

From Dev

Get IEnumerable<SelectListItem> from dbContext

From Dev

How to convert IEnumerable<string> to IList<string> in Dictionary?

From Dev

How to convert IEnumerable<string> to IList<string> in Dictionary?

From Dev

ElasticSearch query failing due to state codes "in" and "or" being reserved words

From Dev

reactjs : unable to map over state due to it being undefined/ error

From Dev

CoreData (+ iCloud) produces invalid model state

From Dev

Model state invalid when using custom RegexAttribute

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

From Dev

Convert existing IEnumerable<int> to string (#,#,#,..) via Method

Related Related

  1. 1

    Convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>

  2. 2

    Convert IDictionary<Guid,string> to the IEnumerable<SelectListItem>

  3. 3

    Convert List to IEnumerable<SelectListItem>

  4. 4

    Making a model being able to convert itself to JSON

  5. 5

    Making a model being able to convert itself to JSON

  6. 6

    Not being able to convert list or string into dict

  7. 7

    Scaffolded model/view: The ViewData item that has the key 'COLUMN' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'

  8. 8

    "There is no ViewData item of type 'IEnumerable<SelectListItem>'" error with custom model and list

  9. 9

    Convert IEnumerable<string> into IEnumerable<FileInfo>

  10. 10

    Not able to convert a string to XML

  11. 11

    Convert IEnumerable<string> to ICollection<string>

  12. 12

    How to convert IEnumerable<char> to IEnumerable<string>?

  13. 13

    CodeKit unable to compile due to not being able to create a cache folder

  14. 14

    CodeKit unable to compile due to not being able to create a cache folder

  15. 15

    SelectListItem Text not being displayed

  16. 16

    how to bind selectlistitem value in dropdownlist to string property in model

  17. 17

    Not able to convert string to decimal in mvc

  18. 18

    Not able to convert string into a char array

  19. 19

    Not able to convert the string to date on Android

  20. 20

    How to add item to IEnumerable SelectListItem

  21. 21

    Get IEnumerable<SelectListItem> from dbContext

  22. 22

    How to convert IEnumerable<string> to IList<string> in Dictionary?

  23. 23

    How to convert IEnumerable<string> to IList<string> in Dictionary?

  24. 24

    ElasticSearch query failing due to state codes "in" and "or" being reserved words

  25. 25

    reactjs : unable to map over state due to it being undefined/ error

  26. 26

    CoreData (+ iCloud) produces invalid model state

  27. 27

    Model state invalid when using custom RegexAttribute

  28. 28

    Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

  29. 29

    Convert existing IEnumerable<int> to string (#,#,#,..) via Method

HotTag

Archive