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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:33:33+00:00 2026-05-27T09:33:33+00:00

I have action methods that do things like this: productTable.AddOrUpdate(viewModel.Account); I don’t currently test

  • 0

I have action methods that do things like this:

productTable.AddOrUpdate(viewModel.Account);

I don’t currently test for if it works. Is there an easy way I can check if this works and if it doesn’t then give an error message and return to my view?

  • 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-27T09:33:33+00:00Added an answer on May 27, 2026 at 9:33 am

    This is not strictly related to MVC, I think it’s a more general issue on how to design code properly to managed unexpected situations (or expected errors generated by user input) and giving appropriate feedback.

    For unexpected behaviours we talk about Exception handling. You put your code in a try/catch block, and handle the exception in the catch.

    For errors that may be generated by user input we’re talking about validation. It means you check your object against a set of rules, and if one or more does not pass, you return a set of errors to your view and display it to the user.

    Now for some MVC specific on how to do this. Let’s have a bit of sample code:

    [HttpPost]
    [ModelStateToTempData]
    public ActionResult SaveMyObject(MyObject myObject)
    {
      try
      {
        if (ModelState.IsValid)
        {
          MyRepository.Save(myObject);
          return RedirectToAction("Success");
        }
        return RedirectToAction("MyForm");
      }
      catch (Exception ex)
      {
        //handle/log your exception as you see fit
        ModelState.AddModelError("Name", "Message");
        return RedirectToAction("MyForm");
      }
    }
    

    In this sample we’re assuming a few things:

    • We’re following the PRG pattern (you can google for it) and implementing it using the ModelStateToTempData attribute from MvcContrib;
    • We’re using a validation method that kicks in during modelbinding (like dataannotations)

    This is a sample method that will receive the post of your form. The data comes from the modelbinder already validated. If the data is valid, you store it and redirect the user to the success page.
    If the data is not valid, you redirect him back to the actionmethod that diplays the form. Careful here: do not render the view directly from here: see the PRG pattern for a full explanation of this.

    The whole thing is in a try/catch block so if something fails, you can trap/handle the error, and then add a custom error message to the page, then redirect the user to the actionmethod that renders the form and display the error to him.

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

Sidebar

Related Questions

I have two action methods that are conflicting. Basically, I want to be able
I have an action like this: public class News : System.Web.Mvc.Controller { public ActionResult
I have a route in my code that looks like this: map.app 'applications/:title_header', :controller
I have an action method that depending on some conditions needs to return a
I have an action method that takes in two different objects. I am posting
I have an ASP.NET MVC project with a form. In the Action method that
ASP.Net MVC Controllers have several methods of forwarding control to another controller or action.
I would like to have 4 actions with the same name (controller methods may
I have a Thread model which has a show page, and in that action's
I have an ajax form I would like to prevent from preforming an action

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.