2013-01-18 57 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

學習的樂趣:) –