Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>In the Catch, if I put Return View(form), I lose the message passed via ViewData.</p> </blockquote> <p>From my understanding, you're not losing the message passed via ViewData - that function simply doesn't run.</p> <p>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.</p> <p>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).</p> <p>WARNING: VB.NET air code from a C# programmer ;-)</p> <pre><code>Function Index() As ActionResult Return IndexView(New MyTable) End Function &lt;ActionName("Index"), AcceptVerbs(HttpVerbs.Post)&gt; _ 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 </code></pre> <p>HTHs, Charles</p> <p>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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload