简体   繁体   中英

creating a drop down list in MVC5

I am trying to create a drop down list but it gives me an error saying 'Cannot implicitly convert type 'string' to 'System.Web.Mvc.SelectList'. My code is below:

Application Database Model:

public string dropdown{ get; set; }

Application View Model:

public SelectList dropdown{ get; set; }

ApplicationService.cs:

 public static SelectList GetDropdownList(string currSelection)
    {
        List<SelectListItem> list = new List<SelectListItem>();
        list.Add(new SelectListItem { Value = "1", Text = "firstvalue" });
        list.Add(new SelectListItem { Value = "2", Text = "secondvalure" });
        list.Add(new SelectListItem { Value = "3", Text = "All of the Above" });


        return new SelectList(list, "Value", "Text", currSelection);
    }

in my controller i am calling:

 applicationviewmodel.dropdown= ApplicationService.GetDropdownList(null);

 and then trying to save it in database as:

 ApplicationDatabaseModel.dropdown= applicationviewmodel.dropdown;

This is where i get this error.

In my view i have:

 @Html.DropDownListFor(x => x.dropdown, applicationviewmodel.dropdown)

I am not sure how to make this work.

I find it's easier to just have a List as part of your model and use a simple linq statement. Simple example below for a countries drop down:

assuming you have a model like

public class MyModel()
{
    public int CountryId { get; set; }
    public List<Country> Countries { get; set; }
}

and a Country class of

public class Country()
{
    public int Id { get; set; }
    public string Name { get; set; }
}

in your view you can then do the following:

@Html.DropDownListFor(m => m.CountryId, 
                           Model.Countries.Select(x => 
                                new SelectListItem { Text = x.Name, Value = x.Id.ToString(), Selected = Model.CountryId == x.Id }, "Please Select...", null)

This:

@Html.DropDownListFor(x => x.dropdown, applicationviewmodel.dropdown)

..is incorrect. It is trying to store the selected item into the SelectList instance.

What you want is a string variable on the view model that this value is selected into:

public class ApplicationViewModel {
    public SelectList DropDown { get; set; }
    public string SelectedDropDownValue { get; set; }
    // .. the rest of the properties here
}

Then your view becomes this:

@Html.DropDownListFor(x => x.SelectedDropDownValue, Model.DropDown)

This says "store the selected value into SelectedDropDownValue ".

Then, you need to change how you build your SelectList . Value is what gets posted to your property.. Text is what is displayed in the browser.

So this:

list.Add(new SelectListItem { Value = "1", Text = "firstvalue" });
list.Add(new SelectListItem { Value = "2", Text = "secondvalure" });
list.Add(new SelectListItem { Value = "3", Text = "All of the Above" });

..has to be this:

list.Add(new SelectListItem { Value = "firstvalue", Text = "firstvalue" });
list.Add(new SelectListItem { Value = "secondvalue", Text = "secondvalure" });
list.Add(new SelectListItem { Value = "all of the above", Text = "All of the Above" });

..because they are strings (unless of course, you want the numbers to be posted back).

Then, finally, your controller code becomes this:

// assign the string value to the string property
ApplicationDatabaseModel.dropdown = applicationviewmodel.SelectedDropDownValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM