2013-01-18 60 views
1

所以我正在构建一个MVC4应用程序,并且我无法让Html.DropDownList表现得很好。所有实际页面显示的都是一个提交按钮,并且它实际上并不呈现下拉列表。MVC4 DropDownList问题

这是控制器:

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

     ViewData["tableitems"] = items; 

     return 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" /> 
} 

我已经好几个小时尝试不同的排列在线搜索,但我无法得到它。任何人都可以指出我做错了什么?

+0

你见过这个吗? http://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor –

+0

这是我正在看的一页,但我仍然无法弄清楚。 –

回答

3

您在的Html.DropDownList

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

前失踪的@没有@,你的代码仅仅是评估一个MvcHtmlString但它实际上没有被渲染到视图,这也正是@进来!

+0

我觉得自己像个白痴......非常感谢你! –

+1

学习的乐趣:) –