user2601893
user2601893

Reputation: 17

viewbag returning null in view and not in controller

Controller

    ViewBag.jsscript = "<script type='text/javascript'>alert('d');</script>";
    return Redirect(refUri + "#/newFile");

In controller it is fine assigning the value

view

@Html.Raw(ViewBag.jsscript)

But in view it is null

Upvotes: 0

Views: 4897

Answers (1)

dotnetom
dotnetom

Reputation: 24901

ViewBag can only be used to provide values from Controller to View in the same request. When you use 'Redirect(...)' there is another request coming from the browser, so your ViewBag value does not exist in that next request.

You can use 'TempData' to store data that survives one request. I'm not sure if you can use it in your code because it is hard to tell the full scope from just a few lines that you provided, but you can try that.

Upvotes: 3

Related Questions