如何使用viewmodel绑定选择列表?

时间:2011-07-08 11:22:14

标签: .net asp.net-mvc-3

我无法将选择列表绑定到我的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>

View会将Question实体的Question_Type设置为所选值,但是当我提交表单时, ValidationMessageFor触发器??

1 个答案:

答案 0 :(得分:14)

你拥有的是不是视图模型。这是一个混合类,你称之为视图模型,并且你已经在其中包装了你的域实体(Question)。这很糟糕,不要这样做。

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

public class QuestionViewModel
{
    [DisplayName("Question_Type")]
    public string SelectedQuestionType { get; set; }

    public IEnumerable<SelectListItem> QuestionTypes { get; set; }

    // didn't see where you are using this on your view
    public string RefUrl { get; set; }
}

然后让您的控制器映射到您的域模型和视图模型之间。当然,进一步的改进是使用AutoMapper来避免整个控制器操作的映射:

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>

最后一句话:确保你已经摆脱了任何ViewBag/ViewData丑陋,并将你的观点需要放到视图模型中。您已在控制器操作中显示了一些类别,这些类别未在您显示的视图片段中显示。如果您需要它们,只需将它们放入视图模型中,就像我们对问题类型所做的那样。