ViewBag.UserStatusList = new List<SelectListItem>
{
new SelectListItem{Text = "Active", Value = "1",Selected=true},
new SelectListItem { Text = "Inactive", Value = "0",Selected=false }
};
上面显示了我绑定到下拉列表的viewbag。所以我想在视图页面中将 Active 修复为默认项目。我该怎么做才能提出一些建议
@Html.DropDownListFor(m => m.UserStatus, (IEnumerable<SelectListItem>)ViewBag.UserStatusList, new { @id = "ddlCustomerStatusList", @class = "form-control", @selected = "selected" })
这是我的观点
答案 0 :(得分:1)
在返回视图之前,需要在GET方法中设置model属性的值
1. Go to Control Panel -> System -> Advanced
2. Click Environment Variables
3. Under System Variables find PATH and click on it.
4. In the Edit windows, find the end of the string of paths (each path needs to be separated by a semi-colon ";" so you may need to add that to the end) add the path to your mysql\bin folder to the end paths.
5. Go back to Desktop
6. Close any command prompts you may have open Got to do this so the new $PATH variable will load.
7. Re-open command prompt. Try running mysql -uroot and if the path was set correctly, mysql will now run.
您可以删除设置model.UserStatus = 1;
return View(model);
的{{1}}属性,因为它被Selected
方法忽略(该方法在内部根据属性的值构建新的SelectListItem
你的绑定)
另请注意,您应删除DropDownListFor()
(这不是IEnumerable<SelectListItem>
元素的有效属性),并且无需使用new { @selected = "selected" }
,因为帮助程序已创建{{1}属性(<select>
)