尽管模型中有必需的标签,但提交的表格

时间:2017-06-13 22:57:32

标签: c# jquery asp.net-mvc forms asp.net-mvc-5

正如问题标题所示,我正在开发一个用户将提交数据的MVC C#应用程序。

按照惯例,我创建了一个模型,并根据需要将几个字段标记为必填字段。然后,如果提交表单,则提交失败并突出显示必填字段。

但是,由于我无法解释的原因,当我提交表单时,表单已提交但由于模型为false而失败。基本上,表单上没有进行验证。 我已经重新创建了项目并重新添加了文件以查看是否可以找到原因但我没有运气,我甚至从一个带有workinng验证的程序中处理了一个创建文件,但我当前的程序仍然跳过它并发生错误。

如果我能在这个问题上得到任何帮助,我很感激。

以下代码

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
        {

            //  [Required(ErrorMessage = "Passport size image is required")]
            [DisplayName("Image")]
            public string 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 (stuimage == null)
                {
                    ModelState.AddModelError("image", "Please Upload an Image");
                }

                if (ModelState.IsValid)
                {

查看

@model Edu_StuConsole_App.Models.Studentinfo


<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"
        });
    });


</script>
   @using (Html.BeginForm("Create", "Student", null, FormMethod.Post, new { 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.ValidationMessageFor(x => x.image)




                                </div>
                            </div>

                            <div class="form-group">


                            <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>

                        <div class="form-group">
                            @Html.LabelFor(model => model.village, "Village", new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.DropDownList("village", null, String.Empty, new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.village)
                            </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>

0 个答案:

没有答案