Tôi không thể có danh sách lựa chọn để liên kết với ViewModel của mình.Làm cách nào để liên kết danh sách lựa chọn với chế độ xem?
Tôi có một ViewModel, trong đó có một tổ chức Câu hỏi và một chuỗi
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 = "";
}
}
này là bộ điều khiển:
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);
}
và mã theo quan điểm của tôi trông như thế này:
<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>
Chế độ xem sẽ đặt Question_Type của đối tượng Câu hỏi thành giá trị đã chọn, nhưng khi tôi gửi biểu mẫu, thứ e ValidationMessageFor triggers ??
Model.Question.Question_Type Model.Question.type_code là 2 thuộc tính khác nhau? bạn có thông báo xác thực cho Question.type_code, nhưng bạn đang đặt Question_Type? –