开发者

How do I pass data between two controllers?

开发者 https://www.devze.com 2023-01-10 18:33 出处:网络
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 ot

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开发者_如何学JAVA = (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>");
%>


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();
    }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消