我無法獲得一個選擇列表來綁定到我的ViewModel。如何將選擇列表與視圖模型綁定?
我有一個包含一個問題實體和字符串
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 = "";
}
}
這是控制器視圖模型:
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);
}
,並在我看來,代碼如下所示:
視圖沒有將Question實體的Question_Type設置爲選定的值,但是當我提交表單時,th e ValidationMessageFor觸發器
Model.Question.Question_Type Model.Question.type_code是2個不同的屬性? 你有Question.type_code的驗證信息,但你正在設置Question_Type? –