0
我有一个html.dropdownlist正在填充索引操作。MVC html.dropdownlist在回发后显示选定的值
public ActionResult Index()
{
var maxyear = (from estiamte in dbBudget.Estimates
select estiamte.Year).Max();
var years = (from estimate in dbBudget.Estimates
select new { estimate.Year }).Distinct().ToList();
ViewData["Years"] = new SelectList(years.OrderByDescending(o => o.Year), "Year", "Year");
var vu_Estimates = dbBudget.vu_Estimates.OrderByDescending(o => o.Expense).ThenBy(o => o.CategoryGroupSortOrder).ThenBy(o => o.SortOrder).Where(o => o.Year == maxyear).ToList();
return View(vu_Estimates);
}
我限定html.dropdownlist使得改变值将触发回传:
<div>
<% using (Html.BeginForm()) { %>
<%= Html.DropDownList("Years", (SelectList)ViewData["Years"], new { onchange = "this.form.submit();" })%> | <%: Html.ActionLink("Create New Year of Estimates", "CreateEstimates", "Estimate") %>
<%} %>
</div>
我捕获回发并过滤所显示的表中。我试图让下拉列表显示选定的值,但这不起作用。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formCollection)
{
var years = (from estimate in dbBudget.Estimates
select new { estimate.Year }).Distinct().ToList();
int year = Convert.ToInt32(formCollection["Years"]);
ViewData["Years"] = new SelectList(years.OrderByDescending(o => o.Year), "Year", "Year", year);
var vu_Estimates = dbBudget.vu_Estimates.OrderByDescending(o => o.Expense).ThenBy(o => o.CategoryGroupSortOrder).ThenBy(o => o.SortOrder).Where(o => o.Year == year).ToList();
return View(vu_Estimates);
}
我需要做什么才能使下拉列表在显示后列出的选定值而不是列表中的第一个值?