C# 正在将novalidate属性添加到表单中
我正在处理一个应用程序,我注意到我的表单验证不起作用,经过一些调查和修复后,它似乎开始起作用,但有一个缺陷,验证只是添加到当前查看的单个选项卡中。C# 正在将novalidate属性添加到表单中,c#,jquery,forms,jquery-validate,C#,Jquery,Forms,Jquery Validate,我正在处理一个应用程序,我注意到我的表单验证不起作用,经过一些调查和修复后,它似乎开始起作用,但有一个缺陷,验证只是添加到当前查看的单个选项卡中。 如果我在另一个选项卡上提交表单,验证将不会启动,它将带我进入错误页面 经过更多的调查,我注意到我的页面上有一个“novalidate”attirbute,就我的一生而言,我无法理解为什么会有它,也无法理解即使在阅读了这个类似的问题后如何删除它 我是如何试图解决这个问题的 [MetadataType(typeof(metaStudent))]
如果我在另一个选项卡上提交表单,验证将不会启动,它将带我进入错误页面 经过更多的调查,我注意到我的页面上有一个“novalidate”attirbute,就我的一生而言,我无法理解为什么会有它,也无法理解即使在阅读了这个类似的问题后如何删除它 我是如何试图解决这个问题的
[MetadataType(typeof(metaStudent))]
public partial class Studentinfo
{
public class metaStudent
{
DisplayName("Image")]
public HttpPostedFileBase image { get; set; }
[Required(ErrorMessage = "First Name is required")]
[DisplayName("First Name")]
public string firstname { get; set; }
[DisplayName("Middle Name")]
[Required(ErrorMessage = "Middle Name is required")]
public string middlename { get; set; }
[Required(ErrorMessage = "Last Name is required")]
[DisplayName("Last Name")]
public string lastname { get; set; }
[DisplayName("Village")]
[Required(ErrorMessage = "Village is required")]
public Nullable<int> village { get; set; }
根据这个问题的指示,我已尝试删除表单上的属性,并确保所显示的代码在web.config
中可用
我还读到,这可能是我的模型中的某个地方出错,所以我注释掉了整个模型,因此没有“必需”字段,基本上我做了各种修补,没有任何东西可以作为NoValidate将自身附加到我的表单上的原因。
我还尝试了一个脚本来启用所有表单的验证,不仅仅是可见表单,而且没有任何更改。
我已经在下面放置了一些代码,请告知我是否遗漏了novalidate存在的原因或我的页面仅在可见选项卡上验证的原因
代码如下
捆绑配置
public class BundleConfig
{
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Content/Gridmvc.js",
"~/Scripts/respond.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/Gridmvc.css",
"~/Content/site.css"));
}
}
}
模型
MetaData.cs类
[MetadataType(typeof(metaStudent))]
public partial class Studentinfo
{
public class metaStudent
{
DisplayName("Image")]
public HttpPostedFileBase image { get; set; }
[Required(ErrorMessage = "First Name is required")]
[DisplayName("First Name")]
public string firstname { get; set; }
[DisplayName("Middle Name")]
[Required(ErrorMessage = "Middle Name is required")]
public string middlename { get; set; }
[Required(ErrorMessage = "Last Name is required")]
[DisplayName("Last Name")]
public string lastname { get; set; }
[DisplayName("Village")]
[Required(ErrorMessage = "Village is required")]
public Nullable<int> village { get; set; }
[MetadataType(typeof(metaStudent))]
公共部分班级学生信息
{
公共班学生
{
显示名称(“图像”)]
公共HttpPostedFileBase映像{get;set;}
[必需(ErrorMessage=“需要名字”)]
[显示名称(“名字”)]
公共字符串名{get;set;}
[显示名称(“中间名称”)]
[必需(ErrorMessage=“需要中间名”)]
公共字符串middlename{get;set;}
[必需(ErrorMessage=“需要姓氏”)]
[显示名称(“姓氏”)]
公共字符串lastname{get;set;}
[显示名称(“村庄”)]
[必需(ErrorMessage=“需要村庄”)]
公共可空村庄{get;set;}
控制器
public async Task<ActionResult> Create([Bind(Include="studentid,firstname,middlename,lastname,villag")] Studentinfo studentinfo, HttpPostedFileBase stuimage)
{
try
{
if (ModelState.IsValid)
{
公共异步任务
@使用(Html.BeginForm(“Create”、“Student”、null、FormMethod.Post、new{name=“frm”、enctype=“multipart/formdata”、@data\u ajax=“false”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@LabelFor(model=>model.image,新的{@class=“controllabel col-md-2”})
@TextBoxFor(m=>m.image,新的{type=“file”})
@Html.ValidationMessageFor(m=>m.image)
@LabelFor(model=>model.studentid,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.studentid,new{htmlAttributes=new{@class=“form control”,@readonly=”“})
@Html.ValidationMessageFor(model=>model.studentid)
@LabelFor(model=>model.firstname,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.firstname,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.firstname)
@LabelFor(model=>model.middlename,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.middlename,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.middlename)
@LabelFor(model=>model.lastname,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.lastname,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.lastname)
布局页面的小药水
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'><![endif]-->
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/modernizr")
...
</div>
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryval")
@RenderSection("scripts", required: false)
</body>
</html>
@style.Render(“~/Content/css”)
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/modernizer”)
...
@Scripts.Render(“~/bundles/bootstrap”)
@Scripts.Render(“~/bundles/jqueryval”)
@RenderSection(“脚本”,必需:false)
“就我个人而言,我不知道它为什么会在那里”~真的吗?你提供的链接清楚地解释了为什么……jQuery验证插件会动态添加此属性。这是为了在使用jQuery验证插件时禁用所有HTML5验证。如果你想使用jQuery验证,那么你将/应该永远不想/需要删除novalidate
属性。有关详细信息,请参阅此更多:@Sparky