简体   繁体   English

在ASP.NET MVC 4中显示DateTime

[英]Show DateTime in ASP.NET MVC 4

I am making a small app and I need to show the DateTime, but I don't know how. 我正在制作一个小应用程序,我需要显示DateTime,但我不知道如何。

I have a ViewModel: 我有一个ViewModel:

public class ViewModel
{
    public DateTime Time{ get; set; }
}

I have a controller called TimeController: 我有一个名为TimeController的控制器:

public ActionResult Index()
{
    ViewModel v = new ViewModel();
    v.Time = DateTime.Now;
    return View(v);
}

And a View from Index: 并从索引视图:

    ViewBag.Title = "Index";
}
<h2>Hello</h2>

Can somebody explain me how I can show the time on my view? 有人可以解释我如何在我的观点上显示时间吗?

Can somebody explain me how i can show the time in my view? 有人可以解释一下我如何在我看来展示时间吗?

You can directly show it using @: 您可以使用@直接显示它:

<div>@DateTime.Now.ToString()</div>

If your only purpose is to show DateTime, I wouldn't bother using ViewModel or atleast wouldn't store DateTime.Now just to be able to display it in View. 如果您的唯一目的是显示DateTime,我不会打扰使用ViewModel或至少不会存储DateTime.Now只是为了能够在View中显示它。

First line of your view 你的观点的第一行

@model ViewModel

This means that your view is typed with type ViewModel 这意味着您的视图使用ViewModel类型键入

Then 然后

@Html.DisplayFor(m => m.Time)

首先在索引视图中添加此顶部:@model Yourapp.Models.ViewModel然后您可以这样做:

Time is : @Model.Time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM