在 ASP.NET Core 中提交表单后如何保持选择的 SelectListItem 值?
How to keep SelectListItem value selected after submitting form in ASP.NET Core?
我正在尝试使用下拉列表过滤 table,但在 ASP.NET Core 中提交表单后我无法保持选中的 SelectListItem 值。
过滤器似乎有效,但每当页面重新加载时,所选项目始终相同
获取控制器
public ActionResult Index(int myDropDown)
{
var myItems= new List<Items>();
myItems = _myRepository.GetAllItems();
var filteredItems = myItems.Where(x => x.Color.DemoId == myDropDown);
if (myDropDown != 0 )
{
return View(filteredItems);
}
return View(myItems);
}
Index.chtml
<form asp-controller="MyController">
<div class="row">
<div class="form-group col-3">
<select class="form-control" id="exampleFormControlSelect1" name="myDropDown"
asp-items="(_myRepository.GetDemos()).Select(x => new SelectListItem()
{ Text = x.Name, Value = x.Id.ToString()})" onchange="this.form.submit()">
</select>
</div>
</form>
C# 方法
public List<Demo> GetDemos()
{
var demos = _dbContext.Demos.Select(x => new Demo()
{
Id = x.Id,
Name = x.Name
}).ToList();
return demos;
}
trying to filter a table using a dropdown-list but I'm not able to keep SelectListItem value selected after submitting the form
您可以尝试动态设置 Selected
属性 of SelectListItem
,如下所示。
<select class="form-control" id="exampleFormControlSelect1" name="myDropDown"
asp-items="(_myRepository.GetDemos()).Select(x => new SelectListItem()
{ Text = x.Name, Value = x.Id.ToString(), Selected = x.Id == ViewBag.SelectedIndex ? true : false })" onchange="this.form.submit()">
</select>
动作方法
public ActionResult Index(int myDropDown)
{
var myItems = new List<Items>();
myItems = _myRepository.GetAllItems();
var filteredItems = myItems.Where(x => x.Color.DemoId == myDropDown);
//pass selected value through ViewBag etc
ViewBag.SelectedIndex = myDropDown;
if (myDropDown != 0)
{
return View(filteredItems);
}
return View(myItems);
}
我正在尝试使用下拉列表过滤 table,但在 ASP.NET Core 中提交表单后我无法保持选中的 SelectListItem 值。
过滤器似乎有效,但每当页面重新加载时,所选项目始终相同
获取控制器
public ActionResult Index(int myDropDown)
{
var myItems= new List<Items>();
myItems = _myRepository.GetAllItems();
var filteredItems = myItems.Where(x => x.Color.DemoId == myDropDown);
if (myDropDown != 0 )
{
return View(filteredItems);
}
return View(myItems);
}
Index.chtml
<form asp-controller="MyController">
<div class="row">
<div class="form-group col-3">
<select class="form-control" id="exampleFormControlSelect1" name="myDropDown"
asp-items="(_myRepository.GetDemos()).Select(x => new SelectListItem()
{ Text = x.Name, Value = x.Id.ToString()})" onchange="this.form.submit()">
</select>
</div>
</form>
C# 方法
public List<Demo> GetDemos()
{
var demos = _dbContext.Demos.Select(x => new Demo()
{
Id = x.Id,
Name = x.Name
}).ToList();
return demos;
}
trying to filter a table using a dropdown-list but I'm not able to keep SelectListItem value selected after submitting the form
您可以尝试动态设置 Selected
属性 of SelectListItem
,如下所示。
<select class="form-control" id="exampleFormControlSelect1" name="myDropDown"
asp-items="(_myRepository.GetDemos()).Select(x => new SelectListItem()
{ Text = x.Name, Value = x.Id.ToString(), Selected = x.Id == ViewBag.SelectedIndex ? true : false })" onchange="this.form.submit()">
</select>
动作方法
public ActionResult Index(int myDropDown)
{
var myItems = new List<Items>();
myItems = _myRepository.GetAllItems();
var filteredItems = myItems.Where(x => x.Color.DemoId == myDropDown);
//pass selected value through ViewBag etc
ViewBag.SelectedIndex = myDropDown;
if (myDropDown != 0)
{
return View(filteredItems);
}
return View(myItems);
}