Passing data (simple string) from Controller to View I have in my controller:
public ActionResult Create()
{
ViewData["user"] = System.Environment.UserName.ToUpper();
return View();
}
and in my cshtml
<h2>Welcome <%=Html.Encode(ViewData["user"]) %></h2>
I checked and verifieid that the username does get populated, however, it does not get displayed on the page.
What am I doing wrong?
Using asp.net mvc 3 with Razor
Those alligator tags (
<%= %>) are from the old ASP.NET view engine.You need to use Razor syntax:
But as other posters pointed out, the Razor engine prefers to encode HTML by default, so all you need is:
If you ever want to not encode something, use the
Html.Raw()method.