asp.net-mvc - MVC 如何检索 View 中的当前日期和时间

标签 asp.net-mvc datetime model-view-controller

我找了好久了,还是没找到答案。我想做的是在创建页面上的两个字段中包含当前日期和当前时间。此页面将用于基本行程安排。

到目前为止我所拥有的是我的模型

public class Journey
{
    public int JourneyId { get; set; }
    public string Name { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",ApplyFormatInEditMode = true)]
    public DateTime Departure { get; set; }
    [DataType(DataType.Time)]
    [DisplayFormat(DataFormatString="{0:hh\\:mm}", ApplyFormatInEditMode = true)]
    public TimeSpan DepartureTime { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime Arrival { get; set; }
    public string Description { get; set; }
    public int FareId { get; set; }
    public int TrainId { get; set; }
    public virtual FareType FareType { get; set; }        
    public virtual Train Train { get; set; }
}    

账户 Controller

public class JourneyController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();
    //
    // GET: Journey
    public ActionResult Index()
    {
        return View(db.Journey.ToList());
    }

    //
    // GET: Journey/Create
    public ActionResult Create()
    {
        ViewBag.TrainId = new SelectList(db.Train, "TrainId", "Name");
        return View();
    }

    // POST: Journey/Create
    [HttpPost]
    public ActionResult Create(Journey model)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.Journey.Add(model);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TrainId = new SelectList(db.Train, "TrainId", "Name", model.TrainId);           
            return RedirectToAction("Index");
        }
        catch (DataException)
        {
            ModelState.AddModelError("", "Sorry something went wrong");
        }
        return View(model);
    }

        }

最后是我的观点

 <div class="form-horizontal">
    <h4>Journey</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Departure, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Departure, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Departure, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DepartureTime, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">                
            @Html.EditorFor(model => model.DepartureTime., new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.DepartureTime, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Arrival, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Arrival, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Arrival, "", new { @class = "text-danger" })
        </div>
    </div>

任何帮助都会很棒 非常感谢

最佳答案

DateTime 属性上使用自定义 getter 和 setter 来提供默认值:

private DateTime? departure;
public DateTime Departure
{
    get { return departure ?? DateTime.Today; }
    set { departure = value; }
}

private DateTime? departureTime;
public DateTime DepartureTime
{
    get { return departureTime ?? DateTime.Now; }
    set { departureTime = value; }
}

private DateTime? arrival;
public DateTime Arrival
{
    get { return arrival ?? DateTime.Today; }
    set { arrival = value; }
}

或者,您可以在操作中手动设置值:

public ActionResult Create()
{
    var journey = new Journey
    {
        Departure = DateTime.Today,
        DepartureTime = DateTime.Now,
        Arrival = DateTime.Today
    };

    ViewBag.TrainId = new SelectList(db.Train, "TrainId", "Name");
    return View(journey);
}

关于asp.net-mvc - MVC 如何检索 View 中的当前日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27905892/

相关文章:

c# - _Layout.cshtml不能直接请求,因为调用了 "RenderBody"方法

javascript - 基于下拉选择的显示/隐藏控件 mvc 4 razor c#

java - jsp 文件不应用链接的 css 文件的样式

php - 检查 mysql 表中的 future 时间

JavaScript 日期怪癖

javascript - html 输入 onchange 不接受匿名函数

php - Codeigniter - if else 内部 View

c# - 在 ASP.NET 中使用缓存

c# - 在 ASP.NET Core 中搭建现有数据库(Scaffold-DbContext)

php如何比较一天与近结束时间的比