3

In our project we have requirement to transfer data from one controller action to another but MVC tempdata is becoming null and not retaining the value between two actions call.

As a work around we are using session object but wanted to check with this forum if there is any potential solution to this problem.

4
  • Are trying to store the data between 2 requests or 2 renderings on the same page? – Richard Seal Oct 23 '17 at 18:28
  • can you please provide more information for what you are trying to do? a code snip will help too – Naim Alkouki Oct 23 '17 at 18:54
  • Code snippet please :-) – Dylan Young Oct 23 '17 at 20:51
  • I am trying to pass data between 2 request. I have two action methods in a controller. I am writing data into tempdata from one action method and trying to read from tempdata in another method. – Tanzil Khan Oct 24 '17 at 6:12
1

If you need to store something for longer than the current and next request I suggest you use Session, a cookie or a database to store your value.

0

In Sitecore pipelines, layout will be rendered first then renderings after. Therefore, you can retrieve Tempdata from Razor view of the layout and pass Tempdata's value as a rendering parameter. See my example below:

In layout view, put this code snippet:

// get data from Tempdata
var dataAsString = TempData["data"];

// pass data into rendering parameter
@Html.Sitecore().Rendering("RenderingPathOrId", new { data = dataAsString })

In rendering view, get your data from rendering parameter:

var dataAsString = RenderingContext.Current.Rendering.Parameters["data"];

Note: Rendering parameter only accept string type so if your Tempdata is an object then you need to Serialize it into Json string.

I know this is not a good way, because you have to put rendering directly in your code, but if you really want to use Tempdata then give it a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.