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

前端之家收集整理的这篇文章主要介绍了.net – 如何使用viewmodel绑定选择列表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法获得一个绑定到我的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触发器?

解决方法

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

这是我建议你的.首先设计一个真实的视图模型,它将反映您视图的要求(从您当前的描述中,它是一个包含一些问题类型的下拉列表,并允许用户从此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的丑陋,并将你的视图需要放到视图模型中.您已在控制器操作中显示了一些类别,这些类别未在您显示的视图片段中显示.如果您需要它们,只需将它们放在视图模型中,就像我们对问题类型所做的那样.

原文链接:https://www.f2er.com/aspnet/251361.html

猜你在找的asp.Net相关文章