Home Ask Login Register

Developers Planet

Your answer is one click away!

user1250264 February 2016

ViewBag.result is null in the View after setting in HttpPost

I have a HttpPost method which I call a submit to database add transcation. If success, I set my viewbag.result = "successfully added"

var response =  UpdateDatabase(command);
if (response.success)
  viewbag.result = "successfully added";


In the view,

if (!string.IsNullOrEmpty(@ViewBag.result))
    <p> @ViewBag.result</p>

I placed a breakpoint and the viewbag.result is null.

I am not sure why the viewbag.result is null. Any help is appreciated. Thanks.


Ric February 2016

You should use TempData["Result"] so that your data can be used after your redirect.

As MSDN states for TempData:

Represents a set of data that persists only from one request to the next.

Also, see more information about usage here:


Sample usage:

TempData["Results"] = "successfully added";

And in your SubmitApplication method:

var message = (string)TempData["Results"];

Always check for nulls etc which I have not done in this example.

Post Status

Asked in February 2016
Viewed 2,559 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life