John Doe
John Doe

Reputation: 259

C# Is it possible to model bind multiple options?

I have a dropdown on which you can select multiple options. enter image description here

The code for this dropdown is: enter image description here

How can i bind multiple 'devices' in C# so that when this dropdown is loaded model binding will automatically select all options which are passed into the view?

Upvotes: 0

Views: 1171

Answers (3)

Alex Art.
Alex Art.

Reputation: 8781

Devices property in your model should be a list of Ids (where is a simple type like int or a string) and not a list of Device models (Since you are using new SelectList(Model.Devices, "ID", "Description") in the Helper it is i see that Model.Devices is a collection of complex object)

So your model should look like:

public List<Device> AvailableDevices { get;set; }

public List<string> Devices { get;set; }

and the Helper should be

@Html.ListBoxFor(m=>m.Devices,new SelectList(Model.AvailableDevices , "ID", "Description"))

or

@Html.DropDownListFor(m=>m.Devices,new SelectList(Model.AvailableDevices , "ID", "Description", new {multiple="multiple"})

post action should receive either a List<string> as an argument or a full model:

[HttpPost]
public ActionResult Submit(List<string> devices)

or

[HttpPost]
public ActionResult Submit(YourModel model) 
//where YourModel model is the same type that you are using to render your view

Upvotes: 1

teo van kot
teo van kot

Reputation: 12491

For your case you should use another helper - @Html.ListBoxFor it should generate select element with multiple attribute.

//note that i use MaintanceDevices property
@Html.ListBoxFor(x => x.MaintanceDevices, new SelectList(Model.Devises, "ID", "Description"), new { @class = "multiselect form-control"})

Also, don't set id attribute in helper. It's better to create another property in your ViewModel:

public List<int> MaintanceDevices { get; set; }

Populate it in Controller and MVC automatically generate right markup for your select element bind in on form POST.

Upvotes: 1

Leandro Soares
Leandro Soares

Reputation: 2972

In this situations, i would do the following inside the viewmodel

public string Devices { get; set; }
List<int> innerList;
public List<int> List
{
    get
    {
        if (this.innerList == null)
        {
            if (string.IsNullOrEmpty(this.Devices))
            {
                this.innerList = this.Devices.Split(',').Select(x => int.Parse(x)).ToList();
            }
            else
            {
                this.innerList = new List<int>();
            }
        }

        return this.innerList;
    }
}

Where Devices is the binded property with the dropdown, which it returns all items separated by ,. When you try to access List it will separate the items and return it as a List<int>.

And i'm parsing it to int because normally i see int's as ID's

But i'm looking forward for a better option.

PS

I do this when working with Select2

Upvotes: 1

Related Questions