如何将下拉列表中的 selectlistitem 值绑定到模型中的字符串 属性
how to bind selectlistitem value in dropdownlist to string property in model
这就是我填充下拉列表和创建 html 元素的方式;
账户控制器:
public ActionResult Index()
{
ViewBag.Towns = new SelectList(_skrsService.GetTowns(), "Value", "Text", 1);
return View();
}
...
public List<SelectListItem> GetTowns()
{
var list = UnitOfWork.Repository<SkrsIl>().OrderBy(x => x.Adi).Select(x => new SelectListItem()
{
Text = x.Adi,
Value = x.Kodu.ToString()
}).ToList();
return list;
}
Login.cshtml(Hometown 是绑定到登录视图页面的模型中的字符串字段):
@Html.DropDownListFor( x => x.Hometown, ((SelectList)ViewBag.Towns), new { @class = "form-control", placeholder = "Doğum Yeri" })
我预计这会起作用,但出现错误,消息:
"The ViewData item that has the key 'Hometown' is of type
'System.String' but must be of type 'IEnumerable<SelectListItem>'."
如何让它正常工作?我只是希望 selectem 项目值 'Hometown' 属性
错误意味着 ViewBag.Towns
是 null
。当第二个参数 (IEnumerable<SelectListItem> selectList
) 为 null
时,该方法期望第一个参数为 typeof IEnumerable<SelectListItem>
.
因为您已经在 GET 方法中分配了它,所以当您提交表单并返回视图但没有像在 GET 方法中那样重新填充 ViewBag.Towns
的值时,几乎肯定会发生这种情况。
附带说明一下,使用 new SelectList()
是毫无意义的额外开销 - 它只是在第一个的基础上创建另一个 IEnumerable<SelectListItem>
。此外,当您绑定到模型 属性 时,SelectList
构造函数的第 4 个参数将被忽略 - 该方法在内部构建一个新的 IEnumerable<SelectListItem>
并设置 Selected
属性 基于 属性 你绑定的值。
你的控制器代码应该是
public ActionResult Index()
{
ViewBag.Towns = _skrsService.GetTowns();
return View(); // ideally you should be returning a model
}
[HttpPost]
public ActionResult Index(YourModel model)
{
if (!ModelState.IsValid)
{
ViewBag.Towns = _skrsService.GetTowns(); // re-populate the SelectList
return View(model);
}
// save and redirect
}
但最好您应该使用包含 属性 的视图模型作为 select 列表而不是 ViewBag
。
这就是我填充下拉列表和创建 html 元素的方式;
账户控制器:
public ActionResult Index()
{
ViewBag.Towns = new SelectList(_skrsService.GetTowns(), "Value", "Text", 1);
return View();
}
...
public List<SelectListItem> GetTowns()
{
var list = UnitOfWork.Repository<SkrsIl>().OrderBy(x => x.Adi).Select(x => new SelectListItem()
{
Text = x.Adi,
Value = x.Kodu.ToString()
}).ToList();
return list;
}
Login.cshtml(Hometown 是绑定到登录视图页面的模型中的字符串字段):
@Html.DropDownListFor( x => x.Hometown, ((SelectList)ViewBag.Towns), new { @class = "form-control", placeholder = "Doğum Yeri" })
我预计这会起作用,但出现错误,消息:
"The ViewData item that has the key 'Hometown' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'."
如何让它正常工作?我只是希望 selectem 项目值 'Hometown' 属性
错误意味着 ViewBag.Towns
是 null
。当第二个参数 (IEnumerable<SelectListItem> selectList
) 为 null
时,该方法期望第一个参数为 typeof IEnumerable<SelectListItem>
.
因为您已经在 GET 方法中分配了它,所以当您提交表单并返回视图但没有像在 GET 方法中那样重新填充 ViewBag.Towns
的值时,几乎肯定会发生这种情况。
附带说明一下,使用 new SelectList()
是毫无意义的额外开销 - 它只是在第一个的基础上创建另一个 IEnumerable<SelectListItem>
。此外,当您绑定到模型 属性 时,SelectList
构造函数的第 4 个参数将被忽略 - 该方法在内部构建一个新的 IEnumerable<SelectListItem>
并设置 Selected
属性 基于 属性 你绑定的值。
你的控制器代码应该是
public ActionResult Index()
{
ViewBag.Towns = _skrsService.GetTowns();
return View(); // ideally you should be returning a model
}
[HttpPost]
public ActionResult Index(YourModel model)
{
if (!ModelState.IsValid)
{
ViewBag.Towns = _skrsService.GetTowns(); // re-populate the SelectList
return View(model);
}
// save and redirect
}
但最好您应该使用包含 属性 的视图模型作为 select 列表而不是 ViewBag
。