The ViewBag object is a
wrapper on ViewData object. So you can access ViewData without data type
converting in html.
You can use strong type data in html view by Viewbag.
Code example:
In controller
public ActionResult
Index()
{
ViewBag.TestName = "teststring";
return View();
}
In cshtml:
<h2>
@ViewBag.TestName
@ViewData["TestName"]
</h2>
@ViewData["TestName"] is equal to @ViewBag.TestName in here
Reference:
You can use strong type data in html view by Viewbag.
It is a new feature in ASP.NET MVC 3 to pass data from Controllers to Views as same as ViewData.
But Viewbag is the dynamic type.
ViewData
<p>
My name is
<b><%: ViewData["name"] %></b>,
<b><%: ViewData["age"] %></b> years old.
<br />
I like the following colors:
</p>
My name is
<b><%: ViewData["name"] %></b>,
<b><%: ViewData["age"] %></b> years old.
<br />
I like the following colors:
</p>
ViewBag
<p>
My name is
<b><%: ViewBag.Name %></b>,
<b><%: ViewBag.Age %></b> years old.
<br />
I like the following colors:
</p>
My name is
<b><%: ViewBag.Name %></b>,
<b><%: ViewBag.Age %></b> years old.
<br />
I like the following colors:
</p>
No comments:
Post a Comment