简体   繁体   中英

Sending Temporary data to multiple partial views from view MVC

I'm wondering if there is a good way to do this. I'm currently trying to send some temporary data to multiple partial views being called from the same view page in my MVC application.

I'm currently attempting to do this with TempData but I can see my understanding is limited as it is only going through for one partial request. What method do I need to use to filter out to all of my partials?

Main View Page:

@{
    ViewBag.Title = "Main View Page";

    TempData["ReturnUrl"] = Request.Url.OriginalString.ToString();
}

@Html.Partial("_StatusTable1")
@Html.Partial("_StatusTable2")
@Html.Partial("_StatusTable3")
@Html.Partial("_StatusTable4")
@Html.Partial("_StatusTable5")

Partial View Example:

@{
    var temp = TempData["ReturnUrl"]; // temp is null on all partials except the first
}

// Partial View Code ...

Thanks in advance.

In your main view page call the partiel views like that

@Html.Partial("_SomePartial", TempData["ReturnUrl"])

I think that even this would work.

@Html.Partial("_SomePartial", TempData)

Get the value from TempData like this. ReturnUrl Value will retained across all the Partial Views

@{
    var temp = TempData.Peek("ReturnUrl");
}

// Partial View Code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM