从 2 个下拉列表获取值的控制器操作
本文关键字:控制器 操作 获取 下拉列表 | 更新日期: 2023-09-27 18:34:24
我有一个操作家庭控制器,它从 2 个下拉列表获取参数。我的目标是保留每个 dropDownList 中的选定值,以便无论何时调用操作,都会定义这两个参数。我的问题是,每次我为其中一个选择新值到下拉列表时,另一个的值都会重置。
控制器:
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")
在这种情况下,
我使用了视图模型和部分视图:
例:
索引视图模型.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(example) 和_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
}