我有一个动作Home控制器,它从2个DropDownLists中获取参数。
我的目标是保留从每个dropDownList中选择的值,以便每当调用该动作时,两个参数都被定义。
我的问题是,每当我为DropDownLists中的一个选择新值时,另一个的值都会被重置。

控制器:

public ActionResult Results ( int? PageSize,int? RaceId, int page = 0)
        {
            ///code that uses PageSize and RaceId to get data from DB
        }

public ActionResult _SelectRaces(int id=3)
        {
            var results = db.Races.Where(r => r.Event_Id == id);
            if (results == null)
            {
                return HttpNotFound();
            }
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var r in results.ToList())
            {
                items.Add(new SelectListItem { Text = r.Name, Value = r.id.ToString() });
            }

            ViewData["Race"] = items;
            return PartialView(results.ToList());
        }

public ActionResult _SelectPageSize()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = "5", Value = "5" });
            items.Add(new SelectListItem { Text = "10", Value = "10" });
            items.Add(new SelectListItem { Text = "25", Value = "25" });

            ViewData["PageSize"] = items;
            return PartialView(items);
        }


观看次数:

_SelectRaces.cshtml

@using (Html.BeginForm())
{
    <fieldset>
        Select Race
        @Html.DropDownList("RaceId", ViewData["Race"] as List<SelectListItem>, new { style = "width: 150px;", onchange = "this.form.submit()" })
    </fieldset>
}


_SelectPAgeSize.cshtml

@using (Html.BeginForm())
{
    <fieldset>
        Select Page Size
        @Html.DropDownList("PageSize", ViewData["PageSize"] as List<SelectListItem>, new { style = "width: 50px;", onchange = "this.form.submit()" })
    </fieldset>
    ViewBag.PAge = "Page";
}


Index.cshtml

    @Html.Action("_SelectPageSize")<!--Select Page Size-->
    @Html.Action("_SelectRaces") <!--Select Race-->
    @Html.Action("Results")

最佳答案

在那种情况下,我使用了一个viewmodel和局部视图:

例:

IndexViewModel.cs

public class IndexViewModel
{
    public IList<SelectListItem> Races { get; set; }
    public int raceSelected { get; set; }
    public IList<SelectListItem> PageSize { get; set; }
    public int pageSizeSelected { get; set; }
}


在您的控制器中:

public ActionResult Index()
    {
        var results = db.Races.Where(r => r.Event_Id == id);
        if (results == null)
        {
            return HttpNotFound();
        }
        var items = new List<SelectListItem>();
        foreach (var r in results.ToList())
        {
            items.Add(new SelectListItem { Text = r.Name, Value = r.id.ToString() });
        }
        var pageSizes = new List<SelectListItem>();
        pageSizes.Add(new SelectListItem { Text = "5", Value = "5" });
        pageSizes.Add(new SelectListItem { Text = "10", Value = "10" });
        pageSizes.Add(new SelectListItem { Text = "25", Value = "25" });

        var model = new IndexViewModel()
        {
            Races = items,
            PageSize = pageSizes
        };
        return View(model);
    }


在您的索引中:

   @this.model IndexViewModel
     @{ Html.RenderPartial("_SelectPageSize", this.Model); }<!--Select Page Size-->
     @{ Html.RenderPartial("_SelectRaces", this.Model); }    <!--Select Race-->
     @{ Html.RenderPartial("Results", this.Model); }


_SelectRaces.cshtml(示例)和_SelectPAgeSize:

@this.model IndexViewModel

    <fieldset>
            Select Race
            @Html.DropDownListFor(x=>x.raceSelected, this.model.Races, new { style = "width: 150px;", onchange = "this.form.submit()" })
        </fieldset>


在提交方法中

    public ActionResult Results (IndexViewModel model)
            {
var raceId = model.raceSelected;
var pageSize = model.pageSizeSelected;

                ///code that uses PageSize and RaceId to get data from DB
            }

10-08 19:00