1

I am trying to poulate a DropDownList with the contents of a Dictionary which is like this:

public static readonly IDictionary<string, string> VideoProviderDictionary = new Dictionary<string, string>
                {
                {"1", "Atlantic"},
                {"2", "Blue Ridge"},
                ...

For the model I have:

public string[] VideoProvider { get; set; }
            public IEnumerable<SelectListItem> Items { get; set; }

In the controller, I am trying to populate the list:

 [HttpGet]
       public ActionResult Register()
       {
            var model = new RegisterViewModel();
            model.Items = new SelectList(VideoProviders.VideoProviderDictionary);

           return View(model);
       }

The issue is in the markup, there is no overload for DropDownList that takes a lambda expression:

 @Html.DropDownList(Model -> model.Items)

I tried to use:

 @Html.DropDownListFor(model => model.Items)

But I get the error:

CS1501: No overload for method 'DropDownListFor' takes 1 arguments
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
user2471435
  • 1,644
  • 7
  • 35
  • 62

2 Answers2

2

In your case -

Model:

public class RegisterViewModel
{

    public static readonly IDictionary<string, string> VideoProviderDictionary = new Dictionary<string, string>
            {{"1", "Atlantic"},
            {"2", "Blue Ridge"}};

    public string VideoProvider { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

Controller:

    [HttpGet]
    public ActionResult Register() {
        var model = new RegisterViewModel();
        model.Items = new SelectList(RegisterViewModel.VideoProviderDictionary, "key", "value");
        return View(model);
    }

View:

    @Html.DropDownListFor(model => model.VideoProvider, Model.Items)
romanoza
  • 4,775
  • 3
  • 27
  • 44
0

Controller :

[HttpGet]
   public ActionResult Register()
   {
        var model = new RegisterViewModel();
        Viewbag.Items = new SelectList(VideoProviders.VideoProviderDictionary);
        ......
        ......
        return View(model);
   }

View :

@Html.DropDownList("VideoProvider",Viewbag.Items as SelectList)

OR For strongly typed dropdownlist do this :

@Html.DropDownListFor(model=>model.VideoProvider,Viewbag.Items as SelectList)

Model:

public string VideoProvider { get; set; }   //Correct here
public IEnumerable<SelectListItem> Items { get; set; }  //if you are using Viewbag to bind dropdownlist(which is a easiest and effective way in MVC) then you don't need any model property for dropdown,you can remove this property.
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
  • Compiler Error Message: CS1501: No overload for method 'DropDownListFor' takes 1 arguments – user2471435 Aug 25 '14 at 17:05
  • Befoe I mark your anser correct, it's displaying each item in the list as [1 - Comcast], etc. How do I modify my controller code to just make it Comcast or whatever the value is in the dictionary? – user2471435 Aug 25 '14 at 17:18
  • I got it. I just set it Value. Thanks – user2471435 Aug 25 '14 at 17:23
  • @user2471435 I don't understand this answer. So, you want to return the value in the "Items" property? – romanoza Aug 25 '14 at 17:32
  • @user2471435..sorry for late reply i have a bad internet connection today.. just see updated answer and correct small issues in model and dropdownlist...model property VideoProvider will have selected dropdown value on post controller.. thanks.. – Kartikeya Khosla Aug 25 '14 at 18:01