1

I am trying to create a multiple select from a single select drop down menu. my model originally had:

public int country_id { get; set; }

and my view had:

 @Html.DropDownList("country_id", String.Empty) 

to change it to multiple select i changed my model to:

public List<Country> country_id { get; set; }

and my view to:

@Html.ListBoxFor(model => model.country_id, ViewBag.ActionsList as MultiSelectList, new { @class = "multiselect", @style = "width: 450px;height:200px" })

the problem i am having is updating my databse using migration since the i am changing int to list, however, it keeps saying

"Cannot drop the index 'dbo.People.IX_country_id', because it does not exist or you do not have permission."

I do have permission so I am not sure if I am missing something?

My list of countries is coming straight from the country database.

thanks for your inputs.

2
  • What is the question about ? The problem with the index or the view? Commented Nov 27, 2014 at 15:47
  • the problem is when i do the database migration to update from int to a list, it is not updating it. so the model doesn't get updated. Commented Nov 27, 2014 at 17:09

1 Answer 1

1

You need to populate a selectlist in the controller & pass it to the view, something like this:

var countries = from d in db.Countries
                        select new
                        {
                            Id = d.Id,
                            Name = d.Name
                        };
// I'd pass this in a model, but use ViewBag if that's what you're familiar with
ViewBag.ActionsList =  new SelectList(countries , "Id", "Name");

And in the View:

@Html.DropDownListFor(model => model.country_id, ViewBag.ActionsList)

UPDATE:

You should use a ViewModel for this:

public class CountryList
{
    // this may have to be a List<SelectListItems> to work with MultiSelectList - check.
    public SelectList Countries{ get; set; }
    public List<int> SelectedCountryIds { get; set; }
}

In the controller:

var model = new CountryList
{
    SelectList = //assign the selectlist created earlier
}
return View(model);

In the View:

@Html.ListBoxFor(m => m.SelectedCountryIds, new MultiSelectList(@Model.Countries, "Id", "Name", @Model.SelectedCountryIds))
Sign up to request clarification or add additional context in comments.

6 Comments

the problem is when i do the database migration to update from int to a list, it is not updating it. so the model doesn't get updated.
Don't update it to a list, that won't work. It needs to stay as an int.
i have another question, so if i am not updating to a list, how do i store/save the selected lists from the drop down?
I see why you were trying update the Id to List - I've update the answer
this is wonderful. so i need my regular database model, and a view model:-) bingo, thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.