The following setup kind of works, but it's not the proper use of TempData, and it falls down after the first use. I suspect I'm supposed to be using ViewData, but I'm looking for some guidance to do it correctly.
In my Home controller, I have an Index action that shows which members need to have letters created:
Function Index() As ActionResult
Dim members = GetMembersFromSomeLongRunningQuery()
TempData("members") = members
Return View(members)
End Function
On the Index view, I've got a link to create the letters:
<%=Html.ActionLink("Create Letters", "CreateLetters", "Home")%>
Back in the Home controller, I've got a CreateLetters function to handle this:
Function CreateLetters() As ActionResult
Dim members = TempData("members")
For Each member In members
'[modify member data regarding the letter]
Next
TempData("members") = members
Return RedirectToAction("Letter", "Home")
End Function
Finally, there is a Letter view which creates the actual letters. It's ContentType is "application/msword", so the data actually comes up in Word instead of the browser.
Like I said, this works great the first time through. However, since the letters come up in Word, when the user comes back to the browser, they are still sitting on the screen with the "Create Letters" link. If they try to hit it again (i.e. paper jam after the first attempt), they get an error in CreateLetters because TempData("members") is now empty. I know this is how TempData works (single shot requests), which is why I'm trying to move away from it.
How would this be refactored to use ViewData? Alternatively, how could I simultaneously display the Letter page (i.e. Word document) and redirect the app back to the Index?