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

2
  • 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<SelectListItem>) Commented Nov 18, 2016 at 21:46
  • Think this link stackoverflow.com/questions/21407508/… gives a better solution to your problem. Commented Nov 21, 2016 at 19:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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")

2 Comments

Thanks will try and let you know
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.