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

Related Questions

This should be a fairly simple question to answer, but I am interested to
This should be a simple question but I haven't managed to find the answer
This seems like a noob question, but the simple answer is eluding me. I
I'm sure that this is a simple newbie question, but the answer is eluding
I realize how simple this question should be to answer but I am in
I feel this should be simple but I can't find an answer. I want
this should be a simple question, but the answer has eluded me for some
This should be a simple answer, but as I'm new to MVC I don't
This should be very simple but I could not find an exhaustive answer: I
I feel like this should have a simple answer, but I can't find it.

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.