Monday, 23 September 2013

display datetime in mvc, show datatime on view in mvc

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.
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
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.
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
<body>
<div>
<%: ViewData["CurrentTime"] %>
</div>
</body>


Or

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


No comments:

Post a Comment