Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6198115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:56:26+00:00 2026-05-24T03:56:26+00:00

How do i update a domain object with ViewModel with AutoMapper using Entity framework?

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:56:26+00:00Added an answer on May 24, 2026 at 3:56 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have controller action in which I update a simple value in domain object:
Is this a valid object design ? I have a domain object where i
Dynamically I want to edit/update hosts(etc/hosts) file to add domain. To edit hosts(etc/hosts) file
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
I have a web page which uses NHibernate to load a domain object. The
I have a domain object used in an identity map (the keys are the
I'm using a domain object as a command object in the web layer. In
I have a JPA domain entity that I'm updating from user input. Depending on
I have an MVC3 project that is using an EF4 project as its Domain.
I want to interrupt some specific grails domain class events(read,write,delete,update).Is there any hibernate eventlistner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.