2015-01-12 38 views
1

我一直在寻找一段时间,似乎无法找到答案。我想要做的是在创建页面上的两个字段中包含当前日期和当前时间。此页面将用于基本的旅程安排。MVC如何检索视图中的当前日期和时间

我至今是我的模型

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

     } 

而且finaly我看来

<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> 

任何帮助将是巨大的 非常感谢

+0

什么的字段类型数据库中的'DepartureTime'? –

回答

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); 
} 
相关问题