Asp.net mvc 要显示和保存的存储时间控制器和视图

Asp.net mvc 要显示和保存的存储时间控制器和视图,asp.net-mvc,model-view-controller,asp.net-mvc-5,Asp.net Mvc,Model View Controller,Asp.net Mvc 5,我有以下课程设置(部分取自): 公共类位置{ [关键] 公共虚拟整数Id{get;set;} 公共虚拟字符串名称{get;set;} 公共虚拟ICollection LocationTimes{get;set;} } 公共类位置时间{ [关键] 公共虚拟整数Id{get;set;} 公共虚拟int LocationId{get;set;} [外键(“位置ID”)] 公共虚拟位置{get;set;} 公共虚拟int-TimeId{get;set;} [外键(“时间ID”)] 公共虚拟时间{get;

我有以下课程设置(部分取自):

公共类位置{
[关键]
公共虚拟整数Id{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟ICollection LocationTimes{get;set;}
}
公共类位置时间{
[关键]
公共虚拟整数Id{get;set;}
公共虚拟int LocationId{get;set;}
[外键(“位置ID”)]
公共虚拟位置{get;set;}
公共虚拟int-TimeId{get;set;}
[外键(“时间ID”)]
公共虚拟时间{get;set;}
}
公共课时间{
公共时间(星期一、星期五、字符串打开时间、字符串关闭时间){
天=天;
开放时间=开放时间??“0000”;
关闭时间=关闭时间??“0000”;
}
[关键]
公共虚拟整数Id{get;set;}
公共虚拟星期日{get;private set;}
公共虚拟字符串OpenTime{get;private set;}
公共虚拟字符串CloseTime{get;private set;}
公共虚拟IEnumerable LocationTImes{get;set;}
公共重写字符串ToString(){
返回$“{Day}:{OpenTime}到{CloseTime}”;
}
}
我想弄清楚的是如何最好地编写我的控制器和视图以如下方式显示,确保尊重任何模型绑定、验证等

目前,如果选择了
关闭
,我不打算保存
时间
值,并在没有值时使应用程序暗示
关闭
,但我不确定我是否喜欢这个想法

另外,示例中的时间间隔为30分钟,这可以/将通过一些应用程序设置进行更改,因此我不想硬编码

如果你需要更多的信息,请告诉我


谢谢。

您的第一个问题是您的“时间”是
string
。虽然.NET没有很好的时间类型,但使用
TimeSpan
是一个更好的选择,它将映射到SqlServer
time
数据类型

您的视图模型应该是

public class LocationTimesCollectionVM
{
    public IEnumerable<SelectListItem> TimeOptions { get; set; }
    public IEnumerable<LocationTimesVM> Days { get; set; }
}
public class LocationTimesVM
{
    public DayOfWeek Day { get; set; }
    [RequiredIfNotEmpty("ClosingTime", ErrorMessage = "An opening time is required if a closing time is specified")]
    public TimeSpan? OpeningTime { get; set; }
    [RequiredIfNotEmpty("OpeningTime", ErrorMessage = "An closing time is required if a opening time is specified")]
    [NotEqualTo("OpeningTime", ErrorMessage = "The closing time cannot equal the opening time")]
    public TimeSpan? ClosingTime { get; set; }
    [DisplayFormat(DataFormatString = "{0:hh\\:mm}")]
    public TimeSpan? OpeningHours { get; set; }
}
您的视图(
Create.cshtml
)将

@model LocationTimesCollectionVM
....
@using (Html.BeginForm())
{
    ....
    <table>
        <thead> ... </thead>
        <tbody>
            @Html.EditorFor(m => m.Days, new { TimeOptions  = Model.TimeOptions })
        </tbody>
    </table>
    ....
}

您遇到的第一个问题是您的“时间”是
string
。虽然.NET没有很好的时间类型,但使用
TimeSpan
是一个更好的选择,它将映射到SqlServer
time
数据类型

您的视图模型应该是

public class LocationTimesCollectionVM
{
    public IEnumerable<SelectListItem> TimeOptions { get; set; }
    public IEnumerable<LocationTimesVM> Days { get; set; }
}
public class LocationTimesVM
{
    public DayOfWeek Day { get; set; }
    [RequiredIfNotEmpty("ClosingTime", ErrorMessage = "An opening time is required if a closing time is specified")]
    public TimeSpan? OpeningTime { get; set; }
    [RequiredIfNotEmpty("OpeningTime", ErrorMessage = "An closing time is required if a opening time is specified")]
    [NotEqualTo("OpeningTime", ErrorMessage = "The closing time cannot equal the opening time")]
    public TimeSpan? ClosingTime { get; set; }
    [DisplayFormat(DataFormatString = "{0:hh\\:mm}")]
    public TimeSpan? OpeningHours { get; set; }
}
您的视图(
Create.cshtml
)将

@model LocationTimesCollectionVM
....
@using (Html.BeginForm())
{
    ....
    <table>
        <thead> ... </thead>
        <tbody>
            @Html.EditorFor(m => m.Days, new { TimeOptions  = Model.TimeOptions })
        </tbody>
    </table>
    ....
}

谢谢伟大而详细的回答!谢谢你的万无一失的参考。谢谢!伟大而详细的回答!谢谢你的万无一失的参考。
@model LocationTimesVM
<tr>
    <td>
        @Html.DisplayFor(m => m.Day)
        @Html.HiddenFor(m => m.Day)
    </td>
    <td>
        @Html.DropDownListFor(m => m.OpeningTime, (IEnumerable<SelectListItem>)ViewData["TimeOptions"], "Closed", new { ... })
        @Html.ValidationMessageFor(m => m.OpeningTime)
    </td>
    <td>
        @Html.DropDownListFor(m => m.ClosingTime, (IEnumerable<SelectListItem>)ViewData["TimeOptions"], "Closed", new { ... })
        @Html.ValidationMessageFor(m => m.ClosingTime)
    </td>
    <td>@Html.DisplayFor(m => m.OpeningHours)</td>
</tr>