StackTrace
StackTrace

Reputation: 9416

Error trying to display a dropdownlist in ASP.NET MVC view

I have below code in my Model

    [NoCache]
    public IEnumerable GatePassTypeList()
    {
        IDictionary<string, IEnumerable<SelectListItem>> gatePassTypelist = new Dictionary<string, IEnumerable<SelectListItem>>();
        List<SelectListItem> gatePassType = new List<SelectListItem>();
        var gatePassData = this.db.GatePassType.OrderBy(r => r.GatePassTypeName).Select(r => r).ToList();
        foreach (var item in gatePassData)
        {
            gatePassType.Add(new SelectListItem { Text = item.GatePassTypeName, Value = item.GatePassTypeID.ToString() });
        }

        gatePassTypelist.Add(string.Empty, gatePassType);
        return gatePassTypelist;
    }

And in my controller i have below Action

 [NoCache]
    public ActionResult GatePassList()
    {
        ViewBag.GatePassTypeList = this.myModel.GatePassTypeList();
        return this.View(this.gatePassEntryModel.GetAllGatePasses());
    }

And finally in my view, i have below code

<td>
@Html.DropDownList("ddlGatePassTypeList", ViewBag.GatePassTypeList as IDictionary<string, IEnumerable<SelectListItem>>, "[All]", new { @multiple = "multiple" })
</td>

What i want to achieve is to display a list of GatePass Types but the above line in my view is throwing the below message in my Elmah error log.

'System.Web.Mvc.HtmlHelper>' does not contain a definition for 'DropDownList' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable, string, object)' has some invalid arguments

Upvotes: 0

Views: 464

Answers (1)

Pranay Rana
Pranay Rana

Reputation: 176906

i guess problem here is you are not taking element out of dictionary, problem is on this line

ViewBag.GatePassTypeList = this.myModel.GatePassTypeList();

because this this.myModel.GatePassTypeList() return dictionary

it should be like , this is not perfect code but thing is you need to pass selectlist after getting item to dropdownlist

   [NoCache]
    public ActionResult GatePassList()
    {
        var dic = this.myModel.GatePassTypeList()
                     as IDictionary<string, IEnumerable<SelectListItem>;
        ViewBag.GatePassTypeList = dic["entrykey"] as List<SelectListItem>;
        return this.View(this.gatePassEntryModel.GetAllGatePasses());
    }

    @Html.DropDownList("Mobiledropdown2", ViewBag.GatePassTypeList as SelectList)  

Upvotes: 1

Related Questions