[RESOLVED]not able to display current time in the View <%: %> tag is not working

Hi all,

I am a rookie in MVC, I tried a simple VieData transition from the controller to the view, but it is not showing me the current time, rather it is showing the tag as it is on the view page.

My controller is like this :

 public class DispplayTimeController : Controller
    {
        //
        // GET: /DispplayTime/

        public ActionResult ShowTime()
        {
            ViewData["CurrentTime"] = DateTime.Now.ToString();
            return View("DisplayTimeView");
        }

    }

And the DisplayTimeView.cshtml is as :

@{
    ViewBag.Title = "Display Time View";
  
    
}

<h2>DisplayTimeView</h2>


<% ViewData["CurrentTime"] %>

when i execute the application i am getting the <% ViewData["CurrentTime"] %> as output rather than the current date and time string.

Please advice on this.

Looking forward to your support.

@{
    ViewBag.Title = "Display Time View";
  
    
}

<h2>DisplayTimeView</h2>


@ViewData["CurrentTime"]

You are using a Razor View. The token for rendering variables to the browser in Razor is @, not <%:

Thanks Mike,

Can you share some documentation, over this, so I don’t make this mistake again?

Leave a Reply