C# 验证ASP.NET MVC表单时回发
我使用asp.net mvc脚手架创建了一个web表单,如果没有回发,它无法进行客户端验证。[Required()]是回发,[EmailAddress]验证器正在客户端进行验证。Im使用visual studio 2013和asp.net mvc 5以及ef6。 此id是我的模型类:C# 验证ASP.NET MVC表单时回发,c#,ajax,asp.net-mvc,C#,Ajax,Asp.net Mvc,我使用asp.net mvc脚手架创建了一个web表单,如果没有回发,它无法进行客户端验证。[Required()]是回发,[EmailAddress]验证器正在客户端进行验证。Im使用visual studio 2013和asp.net mvc 5以及ef6。 此id是我的模型类: namespace WebApplication4.Models { using System; using System.Collections.Generic; using System.ComponentMo
namespace WebApplication4.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Tutor
{
public Tutor()
{
this.Examinations = new HashSet<Examination>();
}
public decimal TutorID { get; set; }
[Display(Name = "First Name ")]
[Required(ErrorMessage = "Please Enter First Name.")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[Display(Name = "Last Name ")]
[Required(ErrorMessage = "Please Enter Last Name.")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[Display(Name = "Address Line 1 ")]
[Required(ErrorMessage = "Please Enter Address Line 1.")]
[DataType(DataType.Text)]
public string Address1 { get; set; }
[Display(Name = "Address Line 2 ")]
[Required(ErrorMessage = "Please Enter Address Line 2.")]
[DataType(DataType.Text)]
public string Address2 { get; set; }
[Display(Name = "Address Line 3 ")]
public string Address3 { get; set; }
[Display(Name = "Telephone 1 ")]
[Required(ErrorMessage = "Please Enter Telephone No.")]
[DataType(DataType.Text)]
public string Tel1 { get; set; }
[Display(Name = "Telephone 2 ")]
[DataType(DataType.Text)]
public string Tel2 { get; set; }
[Display(Name = "Email Address")]
[Required(ErrorMessage = "Please Enter E Mail Address.")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
[DataType(DataType.EmailAddress)]
public string EMail { get; set; }
[Display(Name = "Password ")]
[DataType(DataType.Password)]
public string Password { get; set; }
public Nullable<bool> IsConfirmed { get; set; }
public virtual ICollection<Examination> Examinations { get; set; }
}
}
这是创建的视图
@model WebApplication4.Models.Tutor
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Tutor</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address1, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address1)
@Html.ValidationMessageFor(model => model.Address1)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address2, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address2)
@Html.ValidationMessageFor(model => model.Address2)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address3, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address3)
@Html.ValidationMessageFor(model => model.Address3)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tel1, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tel1)
@Html.ValidationMessageFor(model => model.Tel1)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tel2, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tel2)
@Html.ValidationMessageFor(model => model.Tel2)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EMail, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EMail)
@Html.ValidationMessageFor(model => model.EMail)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model WebApplication4.Models.Tutor
@{
ViewBag.Title=“创建”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
家庭教师
@Html.ValidationSummary(true)
@LabelFor(model=>model.FirstName,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.FirstName)
@Html.ValidationMessageFor(model=>model.FirstName)
@LabelFor(model=>model.LastName,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.LastName)
@Html.ValidationMessageFor(model=>model.LastName)
@LabelFor(model=>model.Address1,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Address1)
@Html.ValidationMessageFor(model=>model.Address1)
@LabelFor(model=>model.Address2,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Address2)
@Html.ValidationMessageFor(model=>model.Address2)
@LabelFor(model=>model.Address3,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Address3)
@Html.ValidationMessageFor(model=>model.Address3)
@LabelFor(model=>model.Tel1,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Tel1)
@Html.ValidationMessageFor(model=>model.Tel1)
@LabelFor(model=>model.Tel2,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Tel2)
@Html.ValidationMessageFor(model=>model.Tel2)
@LabelFor(model=>model.EMail,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.EMail)
@Html.ValidationMessageFor(model=>model.EMail)
@LabelFor(model=>model.Password,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Password)
@Html.ValidationMessageFor(model=>model.Password)
}
@ActionLink(“返回列表”、“索引”)
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
我想验证客户端的所有内容。解决方案是使用jquery.validate.unobtrusive.js 加载表单后,使用jquery on document ready应该解析表单
//file: your view file
@model Tutor
<script>
$(document).ready(function() {
$.validator.unobtrusive.parse($("#frm1"));
}
function onSubmit(e) {
$("#frm1").validate(); // this will validate the form and show the validation messages
if($("#frm1").valid()) {
$("#frm1").submit(); // submits the form
}
return false;//prevent default submit of form by returning false.
//also e.preventDefault() can be used.
}
</script>
//for understanding purpose using the plain form tag.
//one can use @using (Html.BeginForm())
<form id="frm1" onsubmit="onSubmit();">
<!-- your content goes here -->
<div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
</form>
//文件:您的视图文件
@模范导师
$(文档).ready(函数(){
$.validator.unobtrusive.parse($(“#frm1”);
}
提交函数(e){
$(“#frm1”).validate();//这将验证表单并显示验证消息
如果($(“#frm1”).valid()){
$(“#frm1”).submit();//提交表单
}
return false;//通过返回false防止表单的默认提交。
//也可以使用e.preventDefault()。
}
//为了便于理解,请使用普通表单标记。
//可以使用@using(Html.BeginForm())
@LabelFor(model=>model.FirstName,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.FirstName)
@Html.ValidationMessageFor(model=>model.FirstName)
解决方案是使用jquery.validate.unobtrusive.js
加载表单后,使用jquery on document ready应该解析表单
//file: your view file
@model Tutor
<script>
$(document).ready(function() {
$.validator.unobtrusive.parse($("#frm1"));
}
function onSubmit(e) {
$("#frm1").validate(); // this will validate the form and show the validation messages
if($("#frm1").valid()) {
$("#frm1").submit(); // submits the form
}
return false;//prevent default submit of form by returning false.
//also e.preventDefault() can be used.
}
</script>
//for understanding purpose using the plain form tag.
//one can use @using (Html.BeginForm())
<form id="frm1" onsubmit="onSubmit();">
<!-- your content goes here -->
<div class="form-group">
@Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
</form>
//文件:您的视图文件
@模范导师
$(文档).ready(函数(){
$.validator.unobtrusive.parse($(“#frm1”);
}
提交函数(e){
$(“#frm1”).validate();//这将验证表单并显示验证消息
如果($(“#frm1”).valid()){
$(“#frm1”).submit();//提交表单
}
return false;//通过返回false防止表单的默认提交。
//也可以使用e.preventDefault()。
}
//为了便于理解,请使用普通表单标记。
//可以使用@using(Html.BeginForm())
@LabelFor(model=>model.FirstName,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.FirstName)
@Html.ValidationMessageFor(model=>model.FirstName)
确保正确加载jquery.validate.js库
BundleConfig.cs
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
在页面末尾
@Scripts.Render("~/bundles/jqueryval")
确保正确加载jquery.validate.js库 BundleConfig.cs
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
在页面末尾
@Scripts.Render("~/bundles/jqueryval")