0

Index アクションに取り込まれている 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);
    }

リストの最初の値ではなく、ポストバック後に選択した値をドロップダウン リストに表示するにはどうすればよいですか?

4

1 に答える 1

0

私はそれを考え出した。に名前を付ける必要がありHtml.DropdownListます。Yearsたとえば、ddlYears

于 2012-08-10T00:21:47.580 に答える