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 3336998
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:12:07+00:00 2026-05-18T00:12:07+00:00

I have a main Contact and ContactViewModel . How do I get contact model

  • 0

I have a main Contact and ContactViewModel . How do I get contact model and update it to the database ?

  [HttpPost]  
    public ActionResult EditContact(ContactFormViewModel contactView) {  

    }

I was doing like this before I needed ViewModel

[HttpPost]
    public ActionResult EditContact(int id, FormCollection collection) {  
        Contact contact = repository.GetContactById(id);  
                      if (TryUpdateModel(contact, "Contact")) {  
                           repository.Save();  
                           return RedirectToAction("Index");   
 return View(new ContactFormView Model(contact));  
            }   
            }       
  • 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-18T00:12:08+00:00Added an answer on May 18, 2026 at 12:12 am

    It’s a bit easier when you have a view model (you can forget about FormCollection and TryUpdateModel):

    [HttpPost]
    public ActionResult EditContact(ContactViewModel contact) 
    {  
        if (ModelState.IsValid)
        {
            // the model is valid => we can save it to the database
            Contact contact = mapper.Map<ContactViewModel, Contact>(contact);
            repository.Save(contact);  
            return RedirectToAction("Index");   
        }
        // redisplay the form to fix validation errors
        return View(contact);
    }
    

    where mapper converts between the view model and models. AutoMapper is a great choice for this task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the jQuery Validation JS I have two contact forms, the main one
Im building a contact application. In the main activity i have a listview, where
I have a ListView on main activity, which shows a Contact picture on the
We have two applications. Main.exe - which is the main application being updated Update.exe
I have a database, with several tables. The main table, called contacts , stores
Lets say I have a site for a product. Site have three menus: Main,Product,Contact
I have main window which has inner grid components. When I press a button
I have main categories and sub categories. I want to be able to sort
I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
i have main activity in which i have Four menus. and i have one

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.