开发者

How to update a domain object with ViewModel and AutoMapper using Entity framework?

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")
        &l开发者_运维知识库t;/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:

var item = repository.getbyid(model.Id);
_mappingEngine.Map(viewModel, item);
repository.save(item);


When you submit your form, you need to have an action on your controller that will handle the post to the server.

So in addition to the Edit action you currently have, you will need to have another action defined like so:

[HttpPost]
public ActionResult Edit(EditQuestionViewModel model)
{
    //Do the mapping to from your ViewModel to the EF model here

    return View();
}

What this does is sets up a handler so your form can post the data back to the controller and it will bind your fields on your form to the model parameter.

Once you have done this, you can simply map the model back to EF and persist it to the database.

Also, using a bool is perfectly valid and EF will translate and save it as a 0 or 1 in the database for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜