我不知道你在做什麼錯了,因爲你已經證明0的代碼,但這個肯定工程:
public ActionResult Index(int? id)
{
var model = new SelectList(new[]
{
new { Id = 1, Name = "item 1" },
new { Id = 2, Name = "item 2" },
}, "Id", "Name", id);
return View(model);
}
並在您的視圖中:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SelectList>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.DropDownList("abc123", Model) %>
</asp:Content>
現在,如果您導航到/home/index/1
如果您導航到/home/index/2
選定的第二個項目,則選擇第一個項目。
此外,如果您使用ASP.NET MVC 2.0,我會建議您強類型DropDownListFor
幫手,而不是DropDownList
。
在視圖中,暫時註釋掉下拉列表,循環訪問Model.SomeList並確認右邊的項目是「已選中」。 – 2010-07-04 18:13:18
您能否顯示執行您在文章中描述的步驟的控制器操作? – 2010-07-04 19:13:22