MVC 如何在视图中检索当前日期和时间
MVC how to retrieve the current date and time in a view
我已经找了一段时间了,似乎找不到答案。我想要做的是在创建页面的两个字段中包含当前日期和当前时间。此页面将用于基本行程安排。
到目前为止我所拥有的是我的模型
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>
任何帮助都会很棒
非常感谢
在您的 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);
}
我已经找了一段时间了,似乎找不到答案。我想要做的是在创建页面的两个字段中包含当前日期和当前时间。此页面将用于基本行程安排。
到目前为止我所拥有的是我的模型
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>
任何帮助都会很棒 非常感谢
在您的 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);
}