簡體   English   中英

ASP.NET MVC,刪除復雜屬性類型類型上的[必需]

[英]ASP.NET MVC, Remove [Required] on complex properties types types

我想只在某些情況下才需要復雜類型的子屬性。 為此,我正在使用FoolProof庫。

例如,注冊工作周時間表。 我確實有一個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;}
}

如您所見,僅在對應的日期為true時才需要填寫WorkDay類。

但是,驗證始終需要填寫所有時間跨度。 是否可以基於Week POCO類禁用子復雜類型參數內的必需參數?

您不能將驗證屬性應用於復雜屬性並獲得客戶端驗證,因為您不會(也不能)為復雜對象(僅對象的屬性)創建表單控件。 您需要創建一個視圖模型來表示要在視圖中顯示的內容(請注意可為空的屬性)

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" ... />
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM