Sending Temporary data to multiple partial views from view MVC

1.1k views Asked by At

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.

2

There are 2 answers

2
knaos On

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)
1
Augustin On

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

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

// Partial View Code