Asp.net mvc MVC html.dropdownlist在回发后显示所选值
我有一个html.dropdownlist,它正在索引操作中填充Asp.net mvc MVC html.dropdownlist在回发后显示所选值,asp.net-mvc,postback,retain,html-select,Asp.net Mvc,Postback,Retain,Html Select,我有一个html.dropdownlist,它正在索引操作中填充 public ActionResult Index() { var maxyear = (from estiamte in dbBudget.Estimates select estiamte.Year).Max(); var years = (from estimate in dbBudget.Estimates
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>
我需要做什么才能让下拉列表在回发后显示选定的值,而不是列表中的第一个值?我想出来了。我需要将
Html.DropdownList
命名为Years
以外的其他名称,例如ddlYears
[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);
}