ASP.NET MVC,在复杂属性类型类型上删除[必需]

时间:2016-09-28 00:38:20

标签: c# asp.net asp.net-mvc validation complextype

我想在某些环境中只需要复杂类型的子属性。为此我正在使用FoolProof lib。

例如,要注册工作周计划。 我有一个POCO复杂类来存储像这样的时间盘:

[ComplexType]
class workDay{

    [Required]
    public TimeSpan start { get; set; }

    [Required]
    public TimeSpan end { get; set; }

    [Required]
    public TimeSpan interval { get; set; }

}
本周POCO课程的相关部分是:

[ComplexType]
class Week{

    [Required]
    public bool monday { get; set; }
    [RequiredIfTrue("monday")]
    public workDay monday_timespan  {get;set;}

    [Required]
    public bool tuesday { get; set; }
    [RequiredIfTrue("tuesday")]
    public workDay tuesday_timespan {get;set;}

    // ...

    [Required]
    public bool sundat { get; set; }
    [RequiredIfTrue("sunday")]
    public workDay sunday_timespan {get;set;}
}

如您所见,只有当对应的日期为真时,才需要填写WorkDay类。

但是,验证始终要求填写所有时间盘。在某种程度上可以根据Week POCO类禁用子complextype参数中的必需参数吗?

1 个答案:

答案 0 :(得分:2)

您无法将验证属性应用于复杂属性并获得客户端验证,因为您没有(也不能)为复杂对象(仅对象的属性)创建表单控件。您需要创建一个视图模型来表示要在视图中显示的内容(请注意可空属性)

public class DayScheduleVM
{
    public bool IsRequired { get; set; } // this will be used for conditional validation
    [RequiredIf("IsRequired", ErrorMessage = "Please enter the start time")]
    public TimeSpan? StartTime { get; set; }
    [RequiredIf("IsRequired", ErrorMessage = "Please enter the end time")]
    public TimeSpan? EndTime { get; set; }
    [RequiredIf("IsRequired", ErrorMessage = "Please enter the interval")]
    public TimeSpan? Interval { get; set; }
}
public WeekScheduleVM
{
    public DayScheduleVM Sunday { get; set; }
    public DayScheduleVM Monday { get; set; }
    ....
}

并在视图中

@model WeekScheduleVM
....
@using (Html.BeginForm())
{
    <table>
        ....
        <tbody>
            <tr>
                <td>@Html.DisplayNameFor(m => m.Sunday)</td>
                <td>@Html.CheckBoxFor(m => Sunday.IsRequired)</td>
                <td>
                    @Html.TextBoxFor(m => Sunday.StartTime)
                    @Html.ValidationMessageFor(m => Sunday.StartTime)
                </td>
                .... // ditto for EndTime and Interval
            </tr>
            <tr>
                .... // ditto for Monday,Tuesday etc
            </tr>

然后,如果选中该复选框,您将收到一个客户端(和服务器端)错误,相关的StartTimeEndTimeInterval属性未填写(不是明确Interval的用途 - 名称表示其基于StartTimeEndTime的计算值,因此视图中可能不需要这样做

您可以通过向DayOfWeek

添加DayScheduleVM枚举属性来进一步简化此操作并显着减少视图中的代码量
public DayOfWeek Day { get; set; }

这样在您的GET方法中,您可以使用

List<DayScheduleVM> model = new List<DayScheduleVM>();
foreach (var day in Enum.GetValues(typeof(DayOfWeek)))
{
    days.Add(new DayScheduleVM { Day = (DayOfWeek)day });
}
return View(model);

并在视图中

@model List<DayScheduleVM>
....
@using (Html.BeginForm())
{
    <table>
        ....
        <tbody>
            @for(int i = 0; i < Model.Count; i++)
            {
                <tr>
                    <td>@Html.DisplayFor(m => m[i].Day)</td>
                    <td>@Html.CheckBoxFor(m => m[i].IsRequired)</td>
                    <td>
                        @Html.TextboxFor(m => m[i].StartTime)
                        @Html.ValidationMessageFor(m => m[i].StartTime)
                    </td>
                    .... // ditto for `EndTime` and `Interval`
                </tr>
            }
        </tbody>
    </table>
    <input type="submit" ... />
}