我在MVC4表单中使用过jQuery Validation。验证工作正常,但不会发生回发。当我删除jQuery部分时,posback正确发生。但我希望他们两个同时工作。以下是我的代码:
MVC表格:
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data", id = "studentForm" }))
{
<table>
<tr>
<td>
First Name:
</td>
<td>
@Html.TextBoxFor(model => model.FirstName)
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>@Html.TextBoxFor(model => model.LastName)
</td>
</tr>
<tr>
<td>
Gender:
</td>
<td>
@Html.RadioButtonFor(model => model.Gender, "Male") Male
@Html.RadioButtonFor(model => model.Gender, "Female") Female
</td>
</tr>
<tr>
<td>
Email Id:
</td>
<td>
@Html.TextBoxFor(model => model.EmailId)
</td>
</tr>
<tr>
<td>
Phone No:
</td>
<td>
@Html.TextBoxFor(model => model.PhoneNo)
</td>
</tr>
<tr>
<td>
Educational Qualification:
</td>
<td>
@Html.TextBoxFor(model => model.EducationalQualification)
</td>
</tr>
<tr>
<td>
Address:
</td>
<td>
@Html.TextBoxFor(model => model.Address)
</td>
</tr>
<tr>
<td>
Date Of Birth:
</td>
<td>
@Html.TextBoxFor(model => model.DateOfBirth)
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" id="Submit" />
</td>
</tr>
</table>
}
jQuery的:
<script type="text/javascript">
$(document).ready(function () {
$.validator.addMethod("validPhoneNo", function (value, element) {
if (!isNaN(value)) {
return true;
}
else {
return false;
}
}, 'Please enter only digits');
$('#studentForm').validate({
debug: true,
rules: {
FirstName: {
required: true
},
LastName: {
required: true
},
EmailId: {
required: true,
email: true
},
Gender: {
required: true
},
DateOfBirth: {
required: true
},
PhoneNo: {
required: true,
maxlength: 10,
validPhoneNo: true
}
},
messages: {
FirstName: {
required: 'First Name is Required'
},
LastName: {
required: 'Last Name is Required'
},
EmailId: {
required: 'Email Id is Required',
email: 'Please enter a valid email Id'
},
Gender: {
required: 'Gender is required'
},
DateOfBirth: {
required: 'Date Of Birth is required'
},
PhoneNo: {
required: 'Phone No is required',
maxlength: 'Phone no cant be more than 10 digits'
}
}
});
$('#DateOfBirth').datepicker();
});
</script>
控制器:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Home()
{
return View();
}
[HttpPost]
public ActionResult Home(Home model)
{
return RedirectToAction("Display", model);
}
public ActionResult Display(Home model)
{
return View(model);
}
}
这是我的模特:
public class Home
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public string DateOfBirth { get; set; }
public string EmailId { get; set; }
public string PhoneNo { get; set; }
public string EducationalQualification { get; set; }
public string Address { get; set; }
}
任何帮助将不胜感激。