我有一个动作家庭控制器,它从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")
答案 0 :(得分:0)
在这种情况下,我使用了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(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
}