1
我在下拉列表中選擇的值存在問題。如何在下拉列表中選擇真值MVC
查看
@Html.DropDownList("QuestionType", (IEnumerable<SelectListItem>)ViewData["questiontype"], new { id = "QuestionType", @style = "max-width: 200px;width: 200px", @OnChange = "HideForm(this)", @class = "form-control" })
控制器
var questionTypeList = new SelectList(
new List<SelectListItem>
{
new SelectListItem { Value = "1", Text = "Automatic", Selected = false },
new SelectListItem { Value = "2", Text = "Custom", Selected = true}
}, "Value", "Text"
).ToList();
var questionType = new SelectList(questionTypeList, "Value", "Text");
ViewData["questiontype"] = questionType;
我想值= 2在視圖中選擇正確的,但我上面的腳本不工作,我怎樣才能解決這個問題?
我修改了我的代碼。我沒有使用與dropdownlist綁定的強類型。我正在使用viewdata。這是我的代碼:
控制器
List<SelectListItem> questionTypeList = new List<SelectListItem>();
questionTypeList.Add(new SelectListItem() { Text = "Automatic", Value = "1" });
questionTypeList.Add(new SelectListItem() { Selected = true, Text = "Custom", Value = "2" });
ViewData["questiontype"] = questionTypeList;
查看
@Html.DropDownList("QuestionType", ViewData["questiontype"] as SelectList, new { id = "QuestionType", @style = "max-width: 200px;width: 200px", @OnChange = "HideForm(this)", @class = "form-control" })
爲什麼此代碼的工作,爲什麼以前的代碼不能正常工作?我很迷惑......
抱歉..我沒有使用強類型視圖。我只使用viewdata綁定到dropdownlist。我編輯了我的問題,在代碼之前(不工作)和修改後(工作),我很困惑。你能幫忙嗎? –