我有一個數據註釋模型聯繫數據註解似乎不工作
[Required(ErrorMessage = "Please enter a Contact Name")]
public string ContactName
{ get; set; }
[Required(ErrorMessage = "Please enter a Region")]
public string Region
{ get; set; }
我通過這個模型的列表中另一個模型ClientModel現在
[Required(ErrorMessage = "Please enter a name")]
public string clientname
{get;set;}
Public List<Contact> contact
{get;set;}
在我看來,我我結合我ClientModel 這樣
@model MyContact.Models.ClientModel
當我只提交相應的視圖clientname帶有驗證消息。有從List<Contact>
填充的文本框,但它們不驗證。我已經在佈局中複製了所需的腳本,並且還添加了驗證摘要。我不明白爲什麼驗證不會發生。
these are my textboxes
<table border="0" id="tbl2" cellspacing="10" cellpadding="0">
@for (int i = 0; i < Model.ContactList.Count; i++)
{
<tr>
<td>
@Html.CheckBox("chk1", true, new { @class = "BASESAccountTeamChkBoxItem" })
</td>
<td>
@Html.TextBoxFor(m => m.ContactList[i].ContactName, new { style = "width:170px;"})
@Html.ValidationMessageFor((m => m.ContactList[i].ContactName)
</td>
<td>
@Html.TextBoxFor(m => m.ContactList[i].Region, new { style = "width:170px;" })
@Html.ValidationMessageFor((m => m.ContactList[i].ContactName)
</td>
<td>
@Html.TextBoxFor(m => m.ContactList[i].Email, new { style = "width:170px;" })
@Html.ValidationMessageFor((m => m.ContactList[i].Email)
</td>
<td>
@Html.TextBoxFor(m => m.ContactList[i].Phone, style = "width:170px;" })
@Html.ValidationMessageFor((m => m.ContactList[i].Phone)
</td>
<td>
@Html.TextBoxFor(m => m.ContactList[i].Fax, new { style = "width:170px;" })
@Html.ValidationMessageFor((m => m.ContactList[i].Fax)
</td>
</tr>
}
</table>
這是將添加一個新行的JavaScript
+添加新聯繫人
功能addRow(TABLEID){
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
if (table.rows[0].cells[i].innerHTML != "") {
switch (newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
} 當我添加一個新的行驗證不起作用。我不知道爲什麼發生這種情況 而且我只在@Html.Beginform中保留了我的視圖。由於我的觀點是巨大的,我沒有發佈整個代碼。我也有腳本。請幫助