我有一個使用ASP.Net MVC Beta 5的網站,並且我剛將它升級到ASP.Net MVC 1.0。我在下拉列表中選中的項目遇到問題。ASP.Net Html.DropDownList未選擇的元素
後續的人有類似的問題(Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item),但我沒有答案(除了它可能是一個錯誤)
我的控制器方法如下所示:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult View(Guid id)
{
IntegrationLogic logic = new IntegrationLogic(new IntegrationLinq());
CompanyLogic companyLogic = new CompanyLogic(new CompanyLinq());
IntegrationContainer container = new IntegrationContainer();
container.Sources = logic.GetImportSource(id);
container.Companies = companyLogic.GetCompanies(); // Returns a IList<company>
container.SourceActions = logic.GetAllSourceActions(); // Returns an IList<SourceAction>
container.SinkActions = logic.GetAllSinkActions();
container.SuccessActions = logic.GetAllSuccessActions();
container.FailureActions = logic.GetAllFailureActions();
container.Actions = logic.GetAllActions();
container.Watchers = logic.GetAllWatcherActions();
container.ChainActions = logic.GetAllChainActions();
return View("View", container);
}
的觀點是強類型對型號如下
public partial class View : ViewPage<IntegrationContainer> {}
問題區域在視圖模板是:
<label for="Companies">Company: </label><%=Html.DropDownList("Companies",
new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>
我正在創建一個下拉列表,選定的項目從未真正被選中 - 這就是問題所在。 「item.CompanyID」是Guid,「id」是Guid,「name」是在ViewData.Model.Companies實例中保存的IList中提供的公司對象上的字符串。
這實際上是一個錯誤嗎? - 我發現很難理解爲什麼這仍然存在於ASP.Net MVC中......如果這是我所做的事,我會非常高興。
無論如何,建議的解決方法是什麼?
感謝
經過對SO的6個問題的搜索後,這是唯一可行的答案......謝謝! – Martin 2009-11-25 17:51:04