0

In my specific example, I need to pass an error received on one controller to another controller where it will be display. Here is a test case I set up. I've tried TempData, ViewData and Session. One other thing I noticed is that maybe it's the way I'm redirecting. When I put a breakpoint on the receiving controller if I just go to it I hit the breakpoint, but on the redirect it never hits.

Sending Controller Action

public ActionResult New()
    {
        Session["Notice"] = "There was an error";
        Session["NoticeClass"] = "error";
        return RedirectToAction("Index", "Home");
    }

Then here's the receiving controller:

public ActionResult Index()
    {
        //Handle action

        return View();
    }

Then a partial view renders out any errors or notices found

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%
    string Message = "";
    string Class = "hidden";
    if (ViewData["Notice"] != null && ViewData["Notice"] != "")
    {
        Message = (string)ViewData["Notice"];
        Class = (string)ViewData["NoticeClass"];
    }
    if (Session["Notice"] != null && Session["Notice"] != "")
    {
        Message = (string)Session["Notice"];
        Class = (string)Session["NoticeClass"];
        Session["Notice"] = null;
    }

    Response.Write("<div class=\"" + Class + "\" id=\"error_div\"><span id=\"error_span\">" + Message + "</span></div>");
%>
Jhorra
  • 6,233
  • 21
  • 69
  • 123
  • What I've found is that I can set the TempData, Session or ViewData on the receiving controller just fine, but when I set the value of any three of those, then do the redirecttoaction the value appears to be lost every time. – Jhorra Aug 04 '10 at 20:46

2 Answers2

1

UPDATE : Firstly, Sorry but i still cant get a clear picture - assuming you want to get the data in one controller action pass it to another controller's action and then render this in a partial view. You can use Sessions to get the values on the other controller just in a way you stored it....but tempdata i think might also work in your case..then for redirection -

return RedirectToAction("Action","Controller",routevalues)

I think you should read about tempdata and viewdata more here and dont use ViewData unless you have assigned it some value which I can't see in your code and you are still using it.

Tempdata stores value per request....so a new request means it will lose values.Have a look at this if you are looking to pass values using tempdata.

So, in your case if you are only looking to pass a string do something like this -

    public ActionResult New()
    {
        string str = "There was an error";
        return RedirectToAction("Index",str);
    }
    public ActionResult Index(string str)
    {
        Response.Write(str);
        return View();
    }
Community
  • 1
  • 1
Vishal
  • 12,133
  • 17
  • 82
  • 128
  • I actually write it out and format it in a partial view that's used in the master page. It looks for errors and displays them. – Jhorra Aug 04 '10 at 19:57
  • Also I'm actually passing two pieces of data. Also, I'm not just passing to a different action, I'm passing to a completely different controller. – Jhorra Aug 04 '10 at 19:59
  • if you are passing data between controllers- I think you may want to use Sessions. Post more clearly what you want to achieve..have a look at this simple example - http://msdn.microsoft.com/en-us/library/dd394711.aspx – Vishal Aug 04 '10 at 20:03
  • Updated the issue with addition info. – Jhorra Aug 04 '10 at 20:13
  • If I'm setting the error on the same page I'm doing the action I use ViewData, if I'm setting it on another controller, then redirecting it, I use Session. That's why I check for both. – Jhorra Aug 04 '10 at 20:55
1

So apparently there's something specific about redirecting to the root of the site. When I changed the redirect away from /home/index to another action it worked fine. It was only when I redirected to that one that my values disappeared.

Jhorra
  • 6,233
  • 21
  • 69
  • 123