我正在处理一个应用程序,我注意到我的表单验证无效,经过一些调查和修复,它似乎开始工作但有一个缺陷,验证只是被添加到正在查看的单个选项卡。
如果我在另一个标签页上提交表单时,验证将不会触发,它会将我带到错误页面。
经过更多调查,我发现我的网页上有“无效”字样。 attirbute和我的生活,我无法弄清楚为什么它在那里,也不知道如何在阅读这个类似的问题后删除它。
Why is <form> being given NoValidate attribute?
我是如何尝试解决这个问题的
根据此问题的说明,我尝试删除表单上的属性,并确保显示的代码在web.config
中可用。
我还读到,在我的模型中某处可能是一个错误所以我注释掉了整个模型,所以没有&#34;必需&#34;田野,基本上我做了各种修修补补,没有什么能让我感到高兴,因为NoValidate会将自己贴在我的身上。 我还尝试过一个脚本来打开所有表单的验证,而不仅仅是可见的表单,而且没有变化。 我已将下面的一些代码放在下面,如果我可能已经错过了为什么会出现无效,或者为什么我的页面只会在可见选项卡上进行验证,请通知我。
以下代码
BundlesConfig
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; }
控制器
public async Task<ActionResult> Create([Bind(Include="studentid,firstname,middlename,lastname,villag")] Studentinfo studentinfo, HttpPostedFileBase stuimage)
{
try
{
if (ModelState.IsValid)
{
查看
@model Edu_StuConsole_App.Models.STU_studentinfo
@{
ViewBag.Title = "Student";
}
@section scripts {
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
@*<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>*@
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
}
<script type="text/javascript">
$(function () {
$('.datepicker').datepicker({
format: "mm/dd/yyyy",
todayBtn: "linked",
autoclose: true,
maxDate: "+0D",
todayHighlight: true,
orientation: 'top auto',
changeMonth: true,
changeYear: true,
yearRange: "-100:+0"
});
});
$(document).ready(function () {
$('#myform').validate({
ignore: [],
// any other options and/or rules
});
});
$("#frm").validate();
$("#frm").removeAttr("novalidate");
</script>
<div class="panel">
<div class="panel-body">
<h3 class="title-hero">
Add Student
</h3>
<div class="example-box-wrapper">
<ul class="nav-responsive nav nav-tabs">
<li class="active"><a href="#tab1" data-toggle="tab">Personal Information</a></li>
<li>
<a href="#tab_1_2" data-toggle="tab"> Contact Information </a>
</li>
<li>
<a href="#tab_1_3" data-toggle="tab"> Income Information </a>
</li>
</ul>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
@using (Html.BeginForm("Create", "Student", null, FormMethod.Post, new { name = "frm", enctype = "multipart/form-data", @data_ajax = "false" }))
{
@Html.AntiForgeryToken()
<div
class="form-horizontal">
<h4></h4>
@Html.ValidationSummary(true)
<div class="tab-content">
<div class="tab-pane active" id="tab1">
<div class="form-group">
@Html.LabelFor(model => model.image, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="stuimage" id="stuimage" accept="image/*" style=" width: 100%;" onchange="check()" />
@Html.TextBoxFor(m => m.image, new { type = "file" })
@Html.ValidationMessageFor(m => m.image)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.studentid, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.studentid, new { htmlAttributes = new { @class = "form-control", @readonly = "" } })
@Html.ValidationMessageFor(model => model.studentid)
</div>
</div>
<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, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.firstname)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.middlename, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.middlename, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.middlename)
</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, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.lastname)
</div>
</div>
布局小药水页
<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>