5
我試圖讓我的viewmodel設置爲一個表單,它將收集有關加入團隊的人的信息。表格必須包含一些leadup信息,然後列出5個「團隊成員」(每個包含姓名,電子郵件和電話),其中前兩個將是必需的。對於我的驗證,我想它是在每個領域,像這樣:可選複雜類型的[必需]
Person 1:
Name: (validation messaage)
Email: (validation message)
Phone: (validation message)
Person 2:
Name: (validation messaage)
Email: (validation message)
Phone: (validation message)
Person 3:
Name:
Email:
Phone:
Person 4:
Name:
Email:
Phone:
Person 5:
Name:
Email:
Phone:
我的視圖模型的相關部分現:
[Required]
public TeamMember TeamMember1 { get; set; }
[Required]
public TeamMember TeamMember2 { get; set; }
public TeamMember TeamMember3 { get; set; }
public TeamMember TeamMember4 { get; set; }
public TeamMember TeamMember5 { get; set; }
在我看來
所以,我只是寫:
@Html.EditorFor(model=>model.TeamMember1)
@Html.EditorFor(model=>model.TeamMember2)
@Html.EditorFor(model=>model.TeamMember3)
@Html.EditorFor(model=>model.TeamMember4)
@Html.EditorFor(model=>model.TeamMember5)
編輯模板看起來是這樣的:
@model MyProject.Models.TeamMember
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Phone)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Phone)
@Html.ValidationMessageFor(model => model.Phone)
</div>
不好意思把這麼多東西放到一個線程中,但是有人建議如何最好地設置它嗎?我想過從RequiredAttribute繼承並替換TeamMember屬性上的[Required],但我不確定如何在子字段上設置驗證消息。現在,即使它是空的,它也會通過必要的檢查,我假設因爲對象被綁定(並且不是空的),即使所有的屬性都是空白的。
任何反饋意見。