views:

304

answers:

4

Hey all,

Having an odd problems with ASP MVC deployed on IIS6 (Windows 2003). I've simplified the controller code to the below;

<AcceptVerbs(HttpVerbs.Get)> _
Public Function CloseBatches() As ActionResult
    ViewData("Title") = "Close Batches"
    ViewData("Message") = Session("Message")
    Return View()
End Function

<AcceptVerbs(HttpVerbs.Post)> _
Public Function CloseBatches(ByVal RequestId As String) As ActionResult

    Session("Message") = "Yadda yadda blah"

    Return RedirectToAction("CloseBatches")

End Function

The controller did originally do more, of course, but stripped it to this to try to troubleshoot. The page has the basic ViewPage html (master page reference, etc) and then;

<p><%=ViewData("Message")%></p>
<%Using Html.BeginForm("CloseBatches", "Home", New With {.RequestId = "Close"})%>
    <input type="submit" id="Close" value="Close"/>
<%End Using%>

As you can see I'm trying to go with the Post-Redirect-Display pattern which seems to be the way to go at the moment. The trouble is the when you click the button the message doesn't appear, no matter how many times you click the button. However, if you do a refresh/F5 the text does appear - then refresh again and it disappears - refresh again and it appears - repeat!

I've had breakpoints on both controller functions and it hits them at the correct points, I've stepped through the code and no errors are happening so the ViewData should be populated, but the page just doesn't always show it!

Tested with IE7 and FF3 - the latter seems a bit more intermittent in that it does occasionally work!

Any ideas? Something obvious I'm missing? Could some weird caching be going on?

Thanks.

A: 

I don't really know about what problem you are facing, but try to use TempData instead of Session.

Jesper Blad Jensen aka. Deldy
+1  A: 

Change your code as follows:

<AcceptVerbs(HttpVerbs.Get)> _
Public Function CloseBatches() As ActionResult
    ViewData("Title") = "Close Batches"
    Return View()
End Function

<AcceptVerbs(HttpVerbs.Post)> _
Public Function CloseBatches(ByVal RequestId As String) As ActionResult
    TempData("Message") = "Yadda yadda blah"
    Return RedirectToAction("CloseBatches")
End Function

<p><%=ViewData.Eval("Message")%></p>
<%Using Html.BeginForm("CloseBatches", "Home", New With {.RequestId = "Close"})%>
    <input type="submit" id="Close" value="Close"/>
<%End Using%>

Note that ViewData.Eval says "Get the value from ViewData if it's there, or TempData if it isn't." It's the right place to look for Message.

I don't use Session at all in my MVC apps.

Craig Stuntz
A: 

I am facing the same problem, I think I've narrowed it down to occur only when I use RedirectToAction. Sometimes it works, and sometimes it does not. It seems to work as expected in IE7 but in FF3 it isn't.

I have tried both of these methods in my Master Page to check if this value is set

TempData.ContainsKey("Error")

and

ViewData.Eval("Error")
tsquillario
A: 

Sorry, should have mentioned I was originally using TempData but swapped to using Session to make sure the Message data was there for every request to simplify testing. In stepping through it is indeed there, and goes through the ViewData("Message") = Session("Message") bit every time and the debugger shows ViewData as having a Message item in it. The

bit on the page isn't conditional in anyway so it should display said ViewData...but it doesn't, or at least not all the time.

I will look into the .Eval bit to see if that solves anything. Thanks.

W/ .Eval, you can use TempData for redirects, and ViewData for tests. .Eval won't know the difference.
Craig Stuntz