【发布时间】:2011-07-08 19:56:47
【问题描述】:
我无法将选择列表绑定到我的 ViewModel。
我有一个 ViewModel,它包含一个 Question 实体和一个字符串
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);
}
我认为的代码如下所示:
<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>
视图确实将问题实体的 Question_Type 设置为所选值,但是当我提交表单时, ValidationMessageFor 触发器??
【问题讨论】:
-
Model.Question.Question_Type Model.Question.type_code 是 2 个不同的属性?您有 Question.type_code 的验证消息,但您正在设置 Question_Type?
标签: .net asp.net-mvc-3