I can't get a select list to bind to my ViewModel.
I have a ViewModel which contains a Question entity and a string
public class QuestionViewModel
{
public Question Question { get; set; }
public string RefUrl { get; set; }
public QuestionViewModel()
{
}
public QuestionViewModel(Question question, string RefUrl)
{
this.Question = question;
this.RefUrl = RefUrl;
}
public QuestionViewModel(Question question)
{
this.Question = question;
this.RefUrl = "";
}
}
this is the controller:
public ActionResult Edit(int id)
{
Question question = db.Question.Single(q => q.question_id == id);
QuestionViewModel qvm = new QuestionViewModel(question);
ViewBag.category_id = new SelectList(db.Category, "category_id", "category_name", qvm.Question.category_id);
ViewBag.type_code = new SelectList(db.Question_Type, "type_code", "type_description", qvm.Question.type_code);
return View(qvm);
}
and the code in my view looks like this:
<div class="editor-label">
@Html.LabelFor(model => model.Question.type_code, "Question_Type")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => Model.Question.Question_Type, (SelectList)ViewBag.type_code)
@Html.ValidationMessageFor(model => model.Question.type_code)
</div>
The View does set the Question entity's Question_Type to the selected value, but when i submit the form, the
ValidationMessageFor triggers??
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…