views:

495

answers:

2

Hi, so I don't understand what I am doing wrong here. I want to populate a DropDownList inside the master page of my ASP.NET MVC 2 app.

Projects.Master

<div id="supaDiv" class="mainNav">
 <% Html.DropDownList("navigationList"); %>
</div>

MasterController.cs

namespace ProjectsPageMVC.Controllers.Abstracts
{
    public abstract class MasterController : Controller
    {
        public MasterController()
        {
          List<SelectListItem> naviList = new List<SelectListItem>();

          naviList.Add(new SelectListItem
          {
           Selected = true,
           Text = "AdvanceWeb",
           Value = "http://4168web/advanceweb/"
          });

          naviList.Add(new SelectListItem
          {
           Selected = false,
           Text = " :: AdvanceWeb Admin",
           Value = "http://4168web/advanceweb/admin/admindefault.aspx"
          });

          ViewData["navigationList"] = naviList;
        }
    }
}

The DropDownList is not even showing up in the DOM and I am at a loss as to what I am doing wrong.

ProjectsController

namespace ProjectsPageMVC.Controllers
{
    public class ProjectsController : MasterController
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}
+4  A: 

Change

<% Html.DropDownList("navigationList"); %>

to

 <%=Html.DropDownList("navigationList") %>
mxmissile
The equals sign is the difference, though I think you also have to use the overload that includes the SelectList
Dave Swersky
+1 we all do stupid stuff like this :-)
Gabe Moothart
beat me to it +1
curtisk
As long as the data is of type SelectList, you don't need to un-box it, see Dave's answer. Personally I prefer strongly typed data over magic strings.
mxmissile
@Gabe This has bitten me countless times.
mxmissile
this works perfect. Why do I need the "=" and not the ";"???
Tomaszewski
The equals sign indicates a literal value, the semicolon indicates a method call.
Dave Swersky
+2  A: 

Change your markup:

<%= Html.DropDownList("navigationList", (SelectList)ViewData["navigationList"]); %>
Dave Swersky
+1 This is the first complete answer.
Gabe Moothart
this throws an error: Unable to cast object of type 'System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]' to type 'System.Web.Mvc.SelectList'.
Tomaszewski
Your value in the ViewData should be an IEnumerable<SelectListItem>
Dave Swersky