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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:42:22+00:00 2026-06-01T13:42:22+00:00

In a controller a error with Create/Edit ActionResult can be handled with a try-catch

  • 0

In a controller a error with Create/Edit ActionResult can be handled with a try-catch block with the error being displayed on the view (via ModelState.AddModelError).

Now I am trying something similar with the DeleteConfirmed ActionResult but there is no error appearing on the view page. The table I am trying to delete from should be complaining about deleting a foreign-key field value.

Should I RedirectToAction differently or add something else?

 [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                StatusList statuslist = db.Status.Find(id);
                db.Status.Remove(statuslist);
                db.SaveChanges();
            }
            catch (DataException dex)
            {
                ModelState.AddModelError("", dex.Message);
                return RedirectToAction("Delete");

            }
                return RedirectToAction("Index");

        }
  • 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-06-01T13:42:24+00:00Added an answer on June 1, 2026 at 1:42 pm

    If you do a redirect, you lost the ModelState.
    So you can do two things imo.

    1. Setting the error message in TempData[“myerrorkey”] = dex.Message, so the message will “survive” for one redirect
    2. Change your method and, in case of error, return a View so that the model state is not wiped out during the redirect

    Personally I will choose the first. so you can think also to implement TempData in case of a delete telling the user, in the index page, that everything went smooth.

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

Sidebar

Related Questions

Controller First I tried this: [HttpPost] public ActionResult Edit(JournalEntry journalentry) { if (ModelState.IsValid) {
I have the following View, Controller and Comment.cs code, but I get an error,
Hi I have a controller named places with some actions like view, new, create
I have a controller which renders a view (~/Views/Component/Create.aspx) no problem. When this view
I have created Controller Attribute and would like to read SessionId but get error
Whenever I build the following code, I get the error above. //Controller.h #import <UIKit/UIKit.h>
My Controller Action accepts int id. What to do when calling from View ActionLink
In controller level i used [ValidateInput(false)] and ValidateRequest=false in the view. Everything is fine
I am trying to implement Custom Error handling via Action Filter Attributes. My code
In my (Rails 3.2) Test::Unit controller/functional tests, assert_routing is failing with this error: 1)

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.