.net 如何将selectlist与viewmodel绑定?

.net 如何将selectlist与viewmodel绑定?,.net,asp.net-mvc-3,.net,Asp.net Mvc 3,无法获取要绑定到ViewModel的选择列表 我有一个ViewModel,它包含一个问题实体和一个字符串 public class QuestionViewModel { public Question Question { get; set; } public string RefUrl { get; set; } public QuestionViewModel() { } public QuestionViewModel(Quest

无法获取要绑定到ViewModel的选择列表

我有一个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);
    }
在我看来,代码如下所示:

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

@LabelFor(model=>model.Question.type_代码,“Question_type”)
@Html.DropDownListFor(model=>model.Question.Question\u Type,(SelectList)ViewBag.Type\u代码)
@Html.ValidationMessageFor(model=>model.Question.type_代码)
视图确实将问题实体的问题类型设置为所选值,但当我提交表单时
触发器的ValidationMessage???

您拥有的不是视图模型。它是一个混合类,您称之为视图模型,并在其中封装了域实体(
Question
)。那太糟糕了,不要这样做

这是我向你推荐的。首先,设计一个真实的视图模型,该模型将反映视图的要求(根据您当前的描述,它是一个包含一些问题类型的下拉列表,允许用户从此ddl中选择一些问题类型):

然后:

<div class="editor-label">
    @Html.LabelFor(x => x.SelectedQuestionType)
</div>
<div class="editor-field">
    @Html.DropDownListFor(
        x => SelectedQuestionType, 
        new SelectList(Model.QuestionTypes, "Value", "Text")
    )
    @Html.ValidationMessageFor(x => x.SelectedQuestionType)
</div>

@Html.LabelFor(x=>x.SelectedQuestionType)
@Html.DropDownListFor(
x=>SelectedQuestionType,
新的选择列表(Model.QuestionTypes、“Value”、“Text”)
)
@Html.ValidationMessageFor(x=>x.SelectedQuestionType)

最后一句话:确保您已经摆脱了任何
ViewBag/ViewData
丑陋,并将您的视图需要的任何东西放入视图模型中。您已经在控制器操作中显示了一些类别,这些类别在您显示的视图片段中没有具体化。如果您需要它们,只需将它们放在视图模型中,就像我们处理问题类型一样。

model.question.question\u Type model.question.Type\u代码是两个不同的属性?您有Question.type_代码的验证消息,但您正在设置Question_类型?但我需要将问题实体发送到视图,以便编辑它?这样,您只发送包含问题类型和参考的QuestionViewModel_URL@Nanek,不,您不需要向视图发送任何问题实体。正如我所说,需要在视图中编辑的所有属性都必须是视图模型的一部分。然后,您的Post操作将采用相同的视图模型,并将其映射回域问题模型,域问题模型将被发送到存储库进行编辑或任何需要的操作。@Darin,为什么Post上的QuestionTypes为空?Post方法看起来像什么?@mcass20,它是空的,因为提交表单时,
元素只向服务器发送所选的值。这就是HTML的工作原理。在POST操作中,您可以从GET操作中使用的同一存储区填充
QuestionTypes
属性。
public ActionResult Edit(int id)
{
    var question = db.Question.Single(q => q.question_id == id);
    var qvm = new QuestionViewModel
    {
        // preselect a value
        SelectedQuestionType = question.type_code,
        QuestionTypes = db.Question_Type.Select(x => new SelectListItem
        {
            Value = x.type_code,
            Text = x.type_description
        })
    };
    return View(qvm);
}
<div class="editor-label">
    @Html.LabelFor(x => x.SelectedQuestionType)
</div>
<div class="editor-field">
    @Html.DropDownListFor(
        x => SelectedQuestionType, 
        new SelectList(Model.QuestionTypes, "Value", "Text")
    )
    @Html.ValidationMessageFor(x => x.SelectedQuestionType)
</div>