HTML.DropDownListFor 没有将参数传递给控制器?
HTML.DropDownListFor not passing parameter to controller?
我现在正在尝试过滤几个小时的视图。这就是我所拥有的:
我的观点:
@using (Html.BeginForm("Index", "RH_CentroCusto", FormMethod.Get))
{
@Html.DropDownListFor(f => f.DepartmentoSelecionado , Model.Departamentos, "Select")
}
在我的模型中 Class :
public IEnumerable<SelectListItem> Departamentos { get; set; }
public string DepartmentoSelecionado { get; set; }
我的控制器:
//i want to receive the value in departamentoSelecionado !!
public IActionResult Index( string searchString, string departamentoSelecionado = "", int page = 1)
List<SelectListItem> selectList = new List<SelectListItem>();
foreach (var x in _context.RH_Departamento.ToList())
{
selectList.Add(new SelectListItem() { Text = x.Departamento , Value = x.Departamento });
}
//and then i add to the viewModel that i create the list
ViewModelCentroCusto vmcc = new ViewModelCentroCusto()
{
Departamentos = selectList
}
当我尝试 select 一个并点击回车时,它应该是通过的,因为在我的浏览器中我检查过它正在传递查询参数:
但在我的控制器中,我的 departamentoSelecionado 为空,下拉列表也再次获得“select”,而不是保留我之前选择的内容!感谢您的帮助!
将 departamentoSelecionado
更改为 departmentoSelecionado
。您在 departmentoSelecionado
.
中多拼了一个字母 a
//public IActionResult Index( string searchString, string departamentoSelecionado = "", int page = 1)
public IActionResult Index( string searchString, string departmentoSelecionado = "", int page = 1)
动作方法中的参数应与f => f.DepartmentoSelecionado
相同。
2020 年 9 月 16 日更新
控制器代码
public class RH_CentroCustoController : Controller
{
//i want to receive the value in departamentoSelecionado !!
public IActionResult Index(string searchString, string departmentoSelecionado ="", int page = 1)
{
List<SelectListItem> selectList = new List<SelectListItem>();
IList<RH_Departamento> RH_Departamento = new List<RH_Departamento>(){
new RH_Departamento(){ Departamento="dep1" },
new RH_Departamento(){ Departamento="dep2" },
new RH_Departamento(){ Departamento="dep3" },
};//just for test
//foreach (var x in _context.RH_Departamento.ToList())
foreach (var x in RH_Departamento.ToList())
{
selectList.Add(new SelectListItem() { Text = x.Departamento, Value = x.Departamento });
}
//and then i add to the viewModel that i create the list
ViewModelCentroCusto vmcc = new ViewModelCentroCusto()
{
Departamentos = selectList
};
return View(vmcc);
}
}
查看代码
@model xxx.Models.ViewModelCentroCusto
@{
Layout = null;
}
@using (Html.BeginForm("Index", "RH_CentroCusto", FormMethod.Get))
{
@Html.DropDownListFor(f => f.DepartmentoSelecionado, new SelectList(Model.Departamentos, "Value", "Text"), "Select");
<input type="submit" value="Save" />
}
型号代码
public class ViewModelCentroCusto
{
public IEnumerable<SelectListItem> Departamentos { get; set; }
public string DepartmentoSelecionado { get; set; }
}
public class RH_Departamento
{
public string Departamento { get; set; }
}
测试
我现在正在尝试过滤几个小时的视图。这就是我所拥有的: 我的观点:
@using (Html.BeginForm("Index", "RH_CentroCusto", FormMethod.Get))
{
@Html.DropDownListFor(f => f.DepartmentoSelecionado , Model.Departamentos, "Select")
}
在我的模型中 Class :
public IEnumerable<SelectListItem> Departamentos { get; set; }
public string DepartmentoSelecionado { get; set; }
我的控制器:
//i want to receive the value in departamentoSelecionado !!
public IActionResult Index( string searchString, string departamentoSelecionado = "", int page = 1)
List<SelectListItem> selectList = new List<SelectListItem>();
foreach (var x in _context.RH_Departamento.ToList())
{
selectList.Add(new SelectListItem() { Text = x.Departamento , Value = x.Departamento });
}
//and then i add to the viewModel that i create the list
ViewModelCentroCusto vmcc = new ViewModelCentroCusto()
{
Departamentos = selectList
}
当我尝试 select 一个并点击回车时,它应该是通过的,因为在我的浏览器中我检查过它正在传递查询参数:
但在我的控制器中,我的 departamentoSelecionado 为空,下拉列表也再次获得“select”,而不是保留我之前选择的内容!感谢您的帮助!
将 departamentoSelecionado
更改为 departmentoSelecionado
。您在 departmentoSelecionado
.
//public IActionResult Index( string searchString, string departamentoSelecionado = "", int page = 1)
public IActionResult Index( string searchString, string departmentoSelecionado = "", int page = 1)
动作方法中的参数应与f => f.DepartmentoSelecionado
相同。
2020 年 9 月 16 日更新
控制器代码
public class RH_CentroCustoController : Controller
{
//i want to receive the value in departamentoSelecionado !!
public IActionResult Index(string searchString, string departmentoSelecionado ="", int page = 1)
{
List<SelectListItem> selectList = new List<SelectListItem>();
IList<RH_Departamento> RH_Departamento = new List<RH_Departamento>(){
new RH_Departamento(){ Departamento="dep1" },
new RH_Departamento(){ Departamento="dep2" },
new RH_Departamento(){ Departamento="dep3" },
};//just for test
//foreach (var x in _context.RH_Departamento.ToList())
foreach (var x in RH_Departamento.ToList())
{
selectList.Add(new SelectListItem() { Text = x.Departamento, Value = x.Departamento });
}
//and then i add to the viewModel that i create the list
ViewModelCentroCusto vmcc = new ViewModelCentroCusto()
{
Departamentos = selectList
};
return View(vmcc);
}
}
查看代码
@model xxx.Models.ViewModelCentroCusto
@{
Layout = null;
}
@using (Html.BeginForm("Index", "RH_CentroCusto", FormMethod.Get))
{
@Html.DropDownListFor(f => f.DepartmentoSelecionado, new SelectList(Model.Departamentos, "Value", "Text"), "Select");
<input type="submit" value="Save" />
}
型号代码
public class ViewModelCentroCusto
{
public IEnumerable<SelectListItem> Departamentos { get; set; }
public string DepartmentoSelecionado { get; set; }
}
public class RH_Departamento
{
public string Departamento { get; set; }
}