【发布时间】:2019-04-27 07:55:48
【问题描述】:
显然这是一个已经在 SO 上回答的简单问题,但我无法在我的代码中使用它。
我希望能够在我的视图中预先选择 SelectList 上显示的第一个元素。
我的控制器:
public ActionResult Create(){
CostumerRegister rModel = new CostumerRegister();
rModel.AllStores = getStoresForUser(User.Identity.GetUserId());
//to auto select the first element of the select list
rModel.AllStores.First().Selected = true;
return View(rModel);
}
public SelectList getStoresForUser(string userId){
//...
}
在我看来:
@Html.ListBoxFor(m => m.SelectedStores, Model.AllStores, new { @class = "form-control" })
我希望在视图中选择第一个元素,但它似乎没有被选中。
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4 razor