2

ドロップダウンリストの選択した値に問題があります。

意見

@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;

ビューで value=2 を true に選択したいのですが、上記のスクリプトが機能しません。この問題を解決するにはどうすればよいですか?

コードを変更しました。ドロップダウンリストにバインドするstrongtypeを使用していません。ビューデータを使用しています。これは私のコードです:

コントローラ

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" })

なぜこのコードが機能し、なぜ前のコードが機能しないのですか? 私はとても混乱しています...

4

1 に答える 1

1

強く型付けされたビューがあるかどうかはわかりませんが、使用できます

    DropdownlistFor(model=>model.QuestionType, selectlistItem)

次に、コントローラーセットで

model.QuestionType to 2;

それから

return View(model);
于 2014-06-18T03:04:19.927 に答える