Tuesday, 17 June 2014

display current datetime in mvc 4 with asp.net c#



Step 1: Create project and set view data
So the first step is to create a project and a controller. In the controller, set the viewdata variable as shown in the below code snippet and kick off the view.

public class DisplayTimeController : Controller
{
    //
    // GET: /DisplayTime/

    public ActionResult Index()
    {
        ViewData["CurrentTime"] = DateTime.Now.ToString();
        return View();
    }
}
Step 2: Display view data in the view
The next thing is to display data in the view by using the percentage tag. One important point to note is, the view does not have a code-behind. So to display the view we need to use the <%: tag in the ASPX page as shown in the below code snippet.

<body>
<div>
<%: ViewData["CurrentTime"] %>
</div>
</body>


Or

    @Html.Label(DateTime.Now.ToString())

No comments:

Post a Comment