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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:33:44+00:00 2026-05-11T17:33:44+00:00

This should be simple, but the answer is eluding me. If I’ve got a

  • 0

This should be simple, but the answer is eluding me. If I’ve got a Save action in my controller, and the save fails, how do I cancel the action without disturbing what the user entered? For example, Index is strongly-typed of “MyTable”:

Function Index() As ActionResult
    ViewData("message") = "Hello"
    Return View(New MyTable)
End Function

<ActionName("Index"), AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal form As MyTable) As ActionResult
    Try
        SaveMyData(form)
        Return RedirectToAction("Index")
    Catch
        AddModelError("form", "An error occurred.")
        ???
    End Try
End Function

In the Catch, if I put Return View(form), I lose the message passed via ViewData. If I redirect to Index, I’ll lose what the user entered. I think I’ve seen the simple (correct) way to handle this before, but if you don’t know what to search for, it’s hard to find. What am I missing?

  • 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-11T17:33:44+00:00Added an answer on May 11, 2026 at 5:33 pm

    In the Catch, if I put Return View(form), I lose the message passed via ViewData.

    From my understanding, you’re not losing the message passed via ViewData – that function simply doesn’t run.

    And I would highly advise against doing a redirect or using TempData for what you’re trying to achieve, there is just no point in it and not the way MVC is supposed to work.

    Going on what you’ve got I would have a private function that both actions call to return the view. It has your ViewData(“message”) in one place and you still have the previous values in the form (and inside ModelState like Josh E said).

    WARNING: VB.NET air code from a C# programmer 😉

    Function Index() As ActionResult
        Return IndexView(New MyTable)
    End Function
    
    <ActionName("Index"), AcceptVerbs(HttpVerbs.Post)> _
    Function Save(ByVal form As MyTable) As ActionResult
        Try
            SaveMyData(form)
            Return RedirectToAction("Index")
        Catch
            AddModelError("form", "An error occurred.")
        End Try
    
        Return IndexView(form)
    End Function
    
    Private Function IndexView(ByVal form As MyTable) As ActionResult
        ViewData("message") = "Hello"
        Return View(form)
    End Function
    

    HTHs,
    Charles

    Ps. I would just like to add that I find it odd you’ve got an index page that posts back to itself and then if the post has errors it redisplays the index page BUT if there aren’t any errors it redirects back to the index page.

    I don’t know what the app is doing but it does seem like you’re using the index page/view for far too many things.

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

Sidebar

Ask A Question

Stats

  • Questions 99k
  • Answers 99k
  • 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 I feel for a MVC architecture... here should be the… May 11, 2026 at 7:44 pm
  • Editorial Team
    Editorial Team added an answer Soved this by reimplenting the service as a JAX-WS annotated… May 11, 2026 at 7:44 pm
  • Editorial Team
    Editorial Team added an answer I would try it both ways, which will let you… May 11, 2026 at 7:44 pm

Related Questions

This should probably be pretty simple but my Google-Fu is as yet unable to
This has probably been asked before but I can't find any relevant post using
Seems like a simple problem: I have an SVN repo inside our firewall. I
This is a challenge for the css gurus out there. I want to show
I'm sure there are a million posts about this out there, but surprisingly I'm

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.