MVC如何在视图中检索当前日期和时间

时间:2015-01-12 15:54:26

标签: 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; }
}    

帐户控制器

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>

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

1 个答案:

答案 0 :(得分:2)

使用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);
}