0

I am trying to bind the dropdown list to the data-set coming from the data context class in mvc 6. I wrote a function to get the populated list but unable to reproduce the same using razor. Here's what I have so far. Please note that I have not created a model yet. trying to make use of the generated POCO class from the database scaffolding.

function on Layout.cshtml

@functions{

    public List<HSIP.Entities.StateDetails> function1()
    {
        //  protected readonly HSIP.Entities.HSIPContext context;

        HSIP.Entities.HSIPContext hsipcontext = new HSIP.Entities.HSIPContext();
        List<HSIP.Entities.StateDetails> getstatelist = (from s in hsipcontext.StateDetails
                                                   select new HSIP.Entities.StateDetails
                                                   {
                                                       StateDesc = s.StateDesc,
                                                       StateCode = s.StateCode,
                                                       StateAbbr = s.StateAbbr
                                                   }).ToList();
        //SelectList list = new SelectList(getstatelist, "Region", "StateCode", "StateAbbr", "StateDesc");
        return getstatelist;
    }
}

Razor syntax:

@Html.DropDownList("StateDesc", @function1(), "Please select State Name");

The Razor syntax throws an error: there is no argument given that corresponds to the required formal parameter 'htmlattributes' of IHTMLHelper.Dropdownlist(string, IEnumerable, string, object).

can someone please point me in the right direction.

Thanks, Hari

Hari
  • 53
  • 2
  • 10
  • Why in the world are you doing this using `@function` in a view? Its the responsibility of the controller to get the data and pass it to the view. You view should never know anything about your database context and your making your code impossible to unit test. (and the 2nd parameter of `DropDownList()` is `IEnumerable`) –  Nov 18 '16 at 21:46
  • Think this link http://stackoverflow.com/questions/21407508/drop-down-list-at-layout-page-mvc gives a better solution to your problem. – TheFallenOne Nov 21 '16 at 19:47

2 Answers2

3

I am prefer do this:

In a controller/Model:

        using System.Web.Mvc;
        public List<SelectListItem> DropdownListFilter()
        {
            var listitem = new List<SelectListItem>();
            listitem.Add(new SelectListItem { Text = "Dropdown1", Value = "0", Selected = true });
            listitem.Add(new SelectListItem { Text = "Dropdown2", Value = "1", Selected = false });
            listitem.Add(new SelectListItem { Text = "Dropdown3", Value = "2", Selected = false });
            return listitem;
        }

When I Load in the ActionResult Just add this following Line:

ViewBag.FilterDropdown = ar.DropdownListFilter().ToList();

And in the view you have to call Filter dropdown like this:

@Html.DropDownList("FilterDropdown")

Hope this help.

Fityan Aula
  • 82
  • 1
  • 9
1

Firstly use a SelectListItem in your controller and pass it to your view.Then use it in Razor syntax to populate the dropdown.

 List<SelectListItem> stateList = (from s in hsipcontext.StateDetails
                                               select new HSIP.Entities.StateDetails
                                               {
                                                   StateDesc = s.StateDesc,
                                                   StateCode = s.StateCode,
                                                   StateAbbr = s.StateAbbr
                                               }).ToList();

View:

@Html.DropDownListFor("StateDesc", stateList ,"Please select State Name")
TheFallenOne
  • 1,598
  • 2
  • 23
  • 57
  • Thanks will try and let you know – Hari Nov 19 '16 at 15:02
  • I tried your approach but in my view i get the error: The name statelist doesnot exist in the current context. I have to implement this dropdown list binding in the layout view as this should show up across all the pages. Am I missing anything – Hari Nov 19 '16 at 16:26