我启用了客户端验证,实际上它正在工作,我在提交表单时看到了必填字段的错误消息。但是,尽管客户端验证已被解雇,但也发生了回发。我的理解是客户端验证应该抑制回发。有人可以向我确认这是预期的行为,看看这个观点是否有任何不妥之处。非常感谢。
这是有问题的观点
@model Intranet.ViewModels.Student.CreateStudentViewModel
@{
ViewBag.Title = "Create Student";
}
<h2>
Create Student</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<table class="detailViewTable">
<tr>
<td>@Html.LabelFor(c=>c.Contact.Title)</td>
<td>@Html.EditorFor(c=>c.Contact.Title)</td>
<td>@Html.ValidationMessageFor(c=>c.Contact.Title)</td>
</tr>
<tr>
<td>@Html.LabelFor(c=>c.Contact.FirstName)</td>
<td>@Html.EditorFor(c=>c.Contact.FirstName)</td>
<td>@Html.ValidationMessageFor(c=>c.Contact.FirstName)</td>
</tr>
<tr>
<td>@Html.LabelFor(c=>c.Contact.LastName)</td>
<td>@Html.EditorFor(c=>c.Contact.LastName)</td>
<td>@Html.ValidationMessageFor(c=>c.Contact.LastName)</td>
</tr>
<tr>
<td>@Html.LabelFor(c=>c.Contact.Phone)</td>
<td>@Html.EditorFor(c=>c.Contact.Phone)</td>
<td>@Html.ValidationMessageFor(c=>c.Contact.Phone)</td>
</tr>
<tr>
<td>@Html.LabelFor(c=>c.Contact.AltPhone)</td>
<td>@Html.EditorFor(c=>c.Contact.AltPhone)</td>
<td>@Html.ValidationMessageFor(c=>c.Contact.AltPhone)</td>
</tr>
<tr>
<td>@Html.LabelFor(c=>c.Contact.Email)</td>
<td>@Html.EditorFor(c=>c.Contact.Email)</td>
<td>@Html.ValidationMessageFor(c=>c.Contact.Email)</td>
</tr>
<tr>
<td>Guardian 1</td>
<td>@Html.DropDownListFor(c=>c.Guardian1ID, new SelectList(Model.Contacts, "ID", "FullName"))</td>
</tr>
<tr>
<td>Guardian 2</td>
<td>@Html.DropDownListFor(c=>c.Guardian2ID, new SelectList(Model.Contacts, "ID", "FullName"))</td>
</tr>
</table>
<p>
<input type="submit" value="Create" />
</p>
}
<div>
@Html.ActionLink("Back to List", "List")
</div>
这是CreateStudentViewModel
public class CreateStudentViewModel
{
public int ID { get; set; }
public ContactViewModel Contact { get; set; }
public int Guardian1ID { get; set; }
public int Guardian2ID { get; set; }
public List<ContactViewModel> Contacts { get; set; }
}
包含验证属性的ContactViewModel。
public class ContactViewModel
{
public int ID { get; set; }
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }
[Required(ErrorMessage = "First Name is required")]
[DisplayName("First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
[DisplayName("Last Name")]
public string LastName { get; set; }
public string Phone { get; set; }
[DisplayName("Alternate Phone")]
public string AltPhone { get; set; }
public string Email { get; set; }
public string FullName
{
get { return string.Format("{0} {1} {2}", Title, FirstName, LastName); }
}