How do i update a domain object with ViewModel with AutoMapper using Entity framework?
I have a View to edit a Question entity.
This is my Edit action:
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);
}
And my ViewModel looks like this:
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; }
}
This is the View:
@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>
How do i update the entity when i submit the form ?
How should the mapping between the ViewModel and EF Model look like, using AutoMapper?
The properties
public string SelectedCategory { get; set; }
public string SelectedQuestionType { get; set; }
In the ViewModel are supposed to be linked with category_id and type_code in the EF model
Also notice the property
public bool visible { get; set; }
I use BIT in my database. Will this work with the values 0 and 1, which is use in the SelectList?
Thanks!
you would need to get the object from entity framework, and then use automapper like this: