我正在制作一个简单的网络应用程序来帮助我跟踪我在健身房举起的重量。我正在尝试添加一个字段,该字段将自动选择我的锻炼日期,而不会输入任何内容。
例如,如果昨天我在肩膀上工作,我希望它显示为:
日期:2017年12月3日
我尝试在我的视图中添加DateTime.Now.ToLongDateString(),但是这会不断更新当天的日期,因此明天它会显示昨天的肩膀锻炼时间为2017年12月5日。
这是我的模特:
public class Shoulders
{
public int Id { get; set; }
[Display(Name ="Military Press")]
public double MilitaryPress { get; set; }
[Display(Name = "Sides Launch")]
public double SidesLaunch { get; set; }
[Display(Name = "Front Launch")]
public double FrontLaunch { get; set; }
}
这是我的观点:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Date</th>
<th>Military Press</th>
<th>Sides Launch</th>
<th>Front Launch</th>
</tr>
</thead>
<tbody>
@foreach (var workout in Model)
{
<tr>
<td>@DateTime.Now.ToLongDateString()</td>
<td>@workout.MilitaryPress</td>
<td>@workout.SidesLaunch</td>
<td>@workout.FrontLaunch</td>
</tr>
}
</tbody>
答案 0 :(得分:2)
你的班级
public class Shoulders
{
public int Id { get; set; }
[Display(Name ="Military Press")]
public double MilitaryPress { get; set; }
[Display(Name = "Sides Launch")]
public double SidesLaunch { get; set; }
[Display(Name = "Front Launch")]
public double FrontLaunch { get; set; }
}
应改为
public class Shoulders
{
public int Id { get; set; }
// you can change set; to private set; if you add the constructor
public DateTime WorkoutDate { get; set; }
[Display(Name ="Military Press")]
public double MilitaryPress { get; set; }
[Display(Name = "Sides Launch")]
public double SidesLaunch { get; set; }
[Display(Name = "Front Launch")]
public double FrontLaunch { get; set; }
// if you want it set automatically add this constructor
public Shoulders()
{
WorkoutDate = DateTime.Now();
}
}
然后,您可以设置将条目插入数据库的日期
var shoulders = new Shoulders
{
WorkoutDate = DateTime.Now();
//other properties set here and if you added the constructor in the
// class you don't need to set the date here
};
并将其插入数据库。
context.Shoulders.Add(shoulder);
并在您的视图中使用
@model.WorkoutDate.ToShortDateString();