asp.net mvc - multiple select list c# mvc -


i trying create multiple select single select drop down menu. model had:

public int country_id { get; set; } 

and view had:

 @html.dropdownlist("country_id", string.empty)  

to change multiple select changed model to:

public list<country> country_id { get; set; } 

and view to:

@html.listboxfor(model => model.country_id, viewbag.actionslist multiselectlist, new { @class = "multiselect", @style = "width: 450px;height:200px" }) 

the problem having updating databse using migration since changing int list, however, keeps saying

"cannot drop index 'dbo.people.ix_country_id', because not exist or not have permission."

i have permission not sure if missing something?

my list of countries coming straight country database.

thanks inputs.

you need populate selectlist in controller & pass view, this:

var countries = d in db.countries                         select new                         {                             id = d.id,                             name = d.name                         }; // i'd pass in model, use viewbag if that's you're familiar viewbag.actionslist =  new selectlist(countries , "id", "name"); 

and in view:

@html.dropdownlistfor(model => model.country_id, viewbag.actionslist) 

update:

you should use viewmodel this:

public class countrylist {     // may have list<selectlistitems> work multiselectlist - check.     public selectlist countries{ get; set; }     public list<int> selectedcountryids { get; set; } } 

in controller:

var model = new countrylist {     selectlist = //assign selectlist created earlier } return view(model); 

in view:

@html.listboxfor(m => m.selectedcountryids, new multiselectlist(@model.countries, "id", "name", @model.selectedcountryids)) 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -