【发布时间】:2011-07-27 20:05:12
【问题描述】:
如何使用 Entity 框架使用带有 AutoMapper 的 ViewModel 更新域对象?
我有一个可以编辑问题实体的视图。
这是我的编辑操作:
public ActionResult Edit(int id)
{
var question = db.Question.Single(q => q.question_id == id);
Mapper.CreateMap<Question, EditQuestionViewModel>();
EditQuestionViewModel eqvm = Mapper.Map<Question, EditQuestionViewModel>(question);
eqvm.QuestionTypes = new SelectList(db.Question_Type, "type_code", "type_description", question.type_code);
eqvm.Categories = new SelectList(db.Category, "category_id", "category_name", question.category_id);
eqvm.Visibility = new SelectList(new Dictionary<int, string> {
{ 1, "Ja"},
{ 0, "Nej"}
}, "Key", "Value");
return View(eqvm);
}
我的 ViewModel 看起来像这样:
public class EditQuestionViewModel
{
public int question_id { get; set; }
public string question_wording { get; set; }
public bool visible { get; set; }
public int question_number { get; set; }
public string help_text { get; set; }
public Category Category { get; set; }
public Question_Type Question_Type { get; set; }
public string SelectedCategory { get; set; }
public string SelectedQuestionType { get; set; }
public SelectList Categories { get; set; }
public SelectList QuestionTypes { get; set; }
public SelectList Visibility { get; set; }
public string RefUrl { get; set; }
}
这是视图:
@using (Html.BeginForm("Edit", "AdminQuestion", FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Spørgsmål</legend>
<div class="editor-label">
@Html.LabelFor(model => model.question_wording, "Spørgsmål")
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.question_wording, new { @class = "required", rows = 3, cols = 50 })
@Html.ValidationMessageFor(model => model.question_wording)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SelectedCategory, "Hvilken kategori tilhører dette spørgsmål?")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.SelectedCategory, Model.Categories)
@Html.ValidationMessageFor(model => model.SelectedCategory)
</div>
<div class="editor-label">
@Html.LabelFor(x => x.SelectedQuestionType, "Spørgsmålstype")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.SelectedQuestionType, Model.QuestionTypes)
@Html.ValidationMessageFor(model => model.SelectedQuestionType)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.visible, "Skal dette spørgsmål være synligt?")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.visible, Model.Visibility)
@Html.ValidationMessageFor(model => model.visible)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.question_number, "Hvilket nummer har spørgsmålet inden for sin kategori?")
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.question_number, new { @class = "required digits" })
@Html.ValidationMessageFor(model => model.question_number)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.help_text, "Hjælpetekst som hjælper brugeren med at forstå spørgsmålet:")
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.help_text, new { rows = 20, cols = 50 })
@Html.ValidationMessageFor(model => model.help_text)
</div>
<br />
<input type="submit" value="Gem" />
</fieldset>
提交表单时如何更新实体? ViewModel 和 EF Model 之间的映射应该如何使用 AutoMapper?
属性
public string SelectedCategory { get; set; }
public string SelectedQuestionType { get; set; }
ViewModel中应该与EF模型中的category_id和type_code链接
还要注意属性
public bool visible { get; set; }
我在我的数据库中使用 BIT。这是否适用于 SelectList 中使用的值 0 和 1?
谢谢!
【问题讨论】:
标签: asp.net-mvc-3 entity-framework-4