简体   繁体   中英

MVC4 DropDownList issue

So I'm building an MVC4 app, and I cannot get Html.DropDownList to behave at all. All the actual page is showing is a submit button, and it doesn't actually render the drop down list.

This is the controller:

    public ActionResult Index() {
        var items = new SelectList(
            new[] {
                new { Value="ships", Text="Ship" },
            },
            "Value", "Text"
        );

        ViewData["tableitems"] = items;

        return View();
    }

And this is the view:

@using (Html.BeginForm("Search", "Encyclopedia", FormMethod.Get)) {
    @*<select id="table" name="table">
        <option value=""></option>
        <option value="ships">Ship</option>
    </select>*@

    Html.DropDownList("table", ViewData["tableitems"] as SelectList, "Ship" );

    <input type="submit" value="view" />
}

I've been searching online for hours trying various permutations, but I can't get it. Can anyone point out what I'm doing wrong?

You are missing an @ in front of the Html.DropDownList

@using (Html.BeginForm("Search", "Encyclopedia", FormMethod.Get)) {
    @Html.DropDownList("table", ViewData["tableitems"] as SelectList, "Ship" );
    <input type="submit" value="view" />
}

Without the @ , your code is just evaluating to an MvcHtmlString but it's not actually being rendered to the view, that's where the @ comes in!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM