-2

how do i extract the value of tempdata or viewdata in javascript? This returns "" even though there is a value being set. Is the syntax incorrect? I've tried without the '' as well as casting to string. Nothing seems to work...

var message = '@TempData["Message"]';
newbie_86
  • 4,520
  • 17
  • 58
  • 89

1 Answers1

0

This returns "" even though there is a value being set.

Are you sure you have some value in TempData? I think you made some mistake in setting its value then.

Check this and then make sure you are doing it correctly.

  • TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only.
  • Since TempData works this way, you need to know for sure what the next request will be, and redirecting to another view is the only time you can guarantee this.
  • Therefore, the only scenario where using TempData will reliably work is when you are redirecting.This is because a redirect kills the current request , then creates a new request on the server to serve the redirected view.
  • Simply said, Asp.Net MVC TempData dictionary is used to share data between controller actions.
  • The value of TempData persists until it is read or until the current user’s session times out.
  • By default, the TempData saves its content to the session state.
  • TempData values are marked for deletion when you read them. At the end of the request, any marked values are deleted.
  • The benefit is that if you have a chain of multiple redirections it won’t cause TempData to be emptied the values will still be there until you actually use them, then they clear up after themselves automatically.
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79