【发布时间】:2021-08-12 09:39:58
【问题描述】:
我的表单中有这段代码:
<div class="form-group">
@Html.LabelFor(c => c.ClosingHourId)
@Html.DropDownListFor(c => c.ClosingHourId, new SelectList(Model.ClosingHours, "Id", "Time"), "Select time", new { @class = "form-control" })
@Html.ValidationMessageFor(c => c.ClosingHourId)
</div>
“时间”属性的类型为 DateTime,因此当我单击页面上的下拉列表时,我会看到完整的日期和时间,但问题是我只想显示小时数,不显示日期,所以我想使用Time.ToString("H:mm") 之类的东西,但我不知道我在哪里可以写这个,所以它会起作用。也许正确的方法是在我的 ClosingHour 模型中添加类似 [Display(Name = Time.ToString("H:mm"))] 的注释?我不确定这是否可能。
我的视图模型:
public class CinemaFormViewModel
{
public int? Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
[Required]
public string Address { get; set; }
[Required]
[Display(Name = "Total Seats")]
public int TotalSeats { get; set; }
public IEnumerable<OpeningHour> OpeningHours { get; set; }
[Required]
[Display(Name = "Opens At")]
public byte? OpeningHourId { get; set; }
public IEnumerable<ClosingHour> ClosingHours { get; set; }
[Required]
[Display(Name = "Closes At")]
public byte? ClosingHourId { get; set; }
}
我的 ClosingHour 模型:
public class ClosingHour
{
public byte Id { get; set; }
[Required]
public DateTime Time { get; set; }
}
控制器内部调用视图的Action:
public ActionResult New()
{
var openingHours = _context.OpeningHours.ToList();
var closingHours= _context.ClosingHours.ToList();
var viewModel = new CinemaFormViewModel
{
OpeningHours = openingHours,
ClosingHours = closingHours
};
return View("CinemaForm", viewModel);
}
【问题讨论】:
-
你能发布模型和动作来创建视图吗,请
-
我现在已经添加了。
标签: c# asp.net-mvc html.dropdownlistfor selectlist