SKT
SKT

Reputation: 289

ViewData coming up as null

I have this test page. This is the action method.

public ActionResult TestView()
{
    ViewData["Test"] = "1";
    return View("TestView");
}

In TestView.cshtml, I have this line of code

@ViewData["Test"]

It is coming up as null, it is not printing the "1".

Any idea what I am doing wrong?

Thanks.

Upvotes: 0

Views: 340

Answers (1)

vendettamit
vendettamit

Reputation: 14677

MV3 have ViewBag as a new object that can hold the dynamic properties as Key/Value pair.

So you can directly assign a property using ViewBag. For e.g. -

In your action in controller you can write something like

ViewBag.Message = "1";

Now to retrieve the value you can write in your .cshtml

<h2>@ViewBag.Message</h2>

Upvotes: 1

Related Questions