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

  • Home
  • SEARCH
  • 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 174949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:36:48+00:00 2026-05-11T13:36:48+00:00

I have an Action method in an ASP.NET MVC Controller class that handles form

  • 0

I have an Action method in an ASP.NET MVC Controller class that handles form posts from a fairly basic ‘create/edit a user’ page. I’m new to MVC so I’ve been following code samples from various Microsoft tutorials, this is how the method looks currently:

[AcceptVerbs(HttpVerbs.Post)] public ViewResult Save([Bind(Prefix = 'ServiceUser')]ServiceUser SUser) {         if (SUser.ServiceUserId == 0) //new service user             ServiceUserHelper.AddServiceUser(SUser);         else //update to existing service user         {             using (ProjectDataContext db = DatabaseHelper.CreateContext())             {                 this.UpdateModel(db.ServiceUsers.Single(su => su.ServiceUserId == SUser.ServiceUserId), 'ServiceUser');                 db.SubmitChanges();             }         }          //return a confirmation view } 

This works fine; however my instincts tell me that the ‘ProjectDataContext…’ code doesn’t belong in the controller. If I were to move the Update functionality to an other class (in the way I have done with the Insert method), I’d lose the convenience of the Controller’s UpdateModel() method, and probably end up having to do something quite verbose to read the existing entity, update its properties, and submit the changes.

So my question is, what is the best way to achieve this? Is there a method similar to UpdateModel() somewhere in LINQ that can merge two entities of the same type together before submitting?

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. 2026-05-11T13:36:48+00:00Added an answer on May 11, 2026 at 1:36 pm

    Most people will suggest using the ‘Repository Pattern’ to move that data access code out of the controller (and to enable unit testing with mock objects instead of the real database).

    Here are some places to read more:

    • Implementation example for Repository pattern with Linq to Sql and C#
    • Chapter 1 of Professional ASP.NET MVC (Page 32)

    Edit:

    I highly recommend reading the entire Scott Guthrie chapter linked above. It has a wealth of good advice in it. That said, here are some relevant examples (excepted from the chapter)…

    First, I generally like to have different actions for ‘Update’ vs. ‘Add’. Even if they are the same View to render the form, it generally feels cleaner to have different URLs for POSTing an edit vs POSTing a new record. So, here is what the repository pattern in use looks like in a controller’s update action:

    [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, FormCollection formValues) {     //get the current object from the database using the repository class     Dinner dinner = dinnerRepository.GetDinner(id);     try     {         //update the object with the values submitted         UpdateModel(dinner);         //save the changes         dinnerRepository.Save();         //redirect the user back to the read-only action for what they just edited         return RedirectToAction('Details', new { id = dinner.DinnerID });     }     catch     {         //exception occurred, probably from UpdateModel, so handle the validation errors         // (read the full chapter to learn what this extention method is)         ModelState.AddRuleViolations(dinner.GetRuleViolations());         //render a view that re-shows the form with the validation rules shown         return View(dinner);     } } 

    Here is the ‘Add’ example:

    [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create() {     //create a new empty object     Dinner dinner = new Dinner();     try     {         //populate it with the values submitted         UpdateModel(dinner);         //add it to the database         dinnerRepository.Add(dinner);         //save the changes         dinnerRepository.Save();         //redirect the user back to the read-only action for what they just added         return RedirectToAction('Details', new { id = dinner.DinnerID });     }     catch     {         //exception occurred, probably from UpdateModel, so handle the validation errors         // (read the full chapter to learn what this extention method is)         ModelState.AddRuleViolations(dinner.GetRuleViolations());         //render a view that re-shows the form with the validation rules shown         return View(dinner);     } } 

    For both examples above, the DinnerRepository looks like this:

    public class DinnerRepository {     private NerdDinnerDataContext db = new NerdDinnerDataContext();     //     // Query Methods     public IQueryable<Dinner> FindAllDinners()     {         return db.Dinners;     }     public IQueryable<Dinner> FindUpcomingDinners()     {         return from dinner in db.Dinners                where dinner.EventDate > DateTime.Now                orderby dinner.EventDate                select dinner;     }     public Dinner GetDinner(int id)     {         return db.Dinners.SingleOrDefault(d => d.DinnerID == id);     }     //     // Insert/Delete Methods     public void Add(Dinner dinner)     {         db.Dinners.InsertOnSubmit(dinner);     }     public void Delete(Dinner dinner)     {         db.RSVPs.DeleteAllOnSubmit(dinner.RSVPs);         db.Dinners.DeleteOnSubmit(dinner);     }     //     // Persistence     public void Save()     {         db.SubmitChanges();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 90k
  • Answers 90k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can add a template field and drop the textbox… May 11, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer This all depends on how you connect to your repository,… May 11, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer ajaxDotNet plugin uses the jQuery's native ajax functions but it… May 11, 2026 at 6:06 pm

Related Questions

I'm looking for thoughts on how should I use Session in an ASP.NET MVC
I have an ASP.net MVC project in the works at the moment, and I'm
I have an ASP.NET MVC project with a form. In the Action method that
I have an ASP.NET MVC action method which uses model binding to accept a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.