我的难点是如何使用ViewBag
with DropdownListFor
?
在我的控制器中我有:
TestModel model = new TestModel();
ViewBag.Clients = model.Clients;
ViewBag.StatusList = model.StatusList;
ViewBag.enumStatus = model.enumStatus;
ViewBag.intClient = model.intClient;
在我的测试模型中
public SelectList Clients { get; set; }
public SelectList StatusList { get; set; }
public ActiveStatus enumStatus { get; set; }
public int? intClient { get; set; }
在我看来
我想用DropDownListFor
要显示 ViewBag 值,我该怎么做?
你可以这样做:
@Html.DropDownListFor(x => x.intClient, ViewBag.Clients)
但我建议您避免使用 ViewBag/ViewData 并从视图模型中获利:
public ActionResult Index()
{
var model = new TestModel();
model.Clients = new SelectList(new[]
{
new { Value = "1", Text = "client 1" },
new { Value = "2", Text = "client 2" },
new { Value = "3", Text = "client 3" },
}, "Value", "Text");
model.intClient = 2;
return View(model);
}
并在视图中:
@Html.DropDownListFor(x => x.intClient, Model.Clients)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)