为什么 mvc 验证在我的代码中不起作用?

why mvc validation does not work in my code?

我有一个简单的 MVC 代码,其表单带有 validation.but 我对名称 属性(必需)的验证不会 work.I 在 "InsertStudent" 操作后在我的控制器中创建断点结果。

所以我 运行 填写表格而不填写名称文本框,我希望我的控制器中的 "IsValid" 是 false.but 这是真的!!!

我的观点是:

 @model HelloWorld.Models.student

    @{
        Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>AddStudent2</title>
    </head>
    <body>
        <script src="~/Scripts/jquery-1.8.2.min.js"></script>
        <script src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

        @using (Html.BeginForm("InsertStudent","home",FormMethod.Post)) {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

            <fieldset>
                <legend>student</legend>

                <div class="editor-label">
                   @Html.LabelFor(model => model.Name)
                   @Html.ValidationMessageFor(model => model.Name)
               </div>
               <div class="editor-field">
                    @Html.EditorFor(model => model.Name)
                     @Html.ValidationMessageFor(model => model.Name)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Family)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Family)
                    @Html.ValidationMessageFor(model => model.Family)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Age)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Age)
                    @Html.ValidationMessageFor(model => model.Age)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Email)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Email)
                    @Html.ValidationMessageFor(model => model.Email)
                </div>

                <p>
                       <input type="submit" value="Create" />
                </p>
            </fieldset>
        }

        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>
    </body>
    </html>

这是我的控制器:

 public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult index()
        {
            return View("home");
        }
        public ActionResult addstudent()
        {
            student stud = new student();
            return View(stud);
        }

        public ActionResult InsertStudent(student stud)
        {
            if (ModelState.IsValid==true)
           {
              //sahih
           }
            else{
               //ghalat
            }


            return View("addstudent");
        }

        public ActionResult AddStudent2()
        {

            student stud2 = new student();
            return View(stud2);
        }

    }
}

模型是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace HelloWorld.Models
{
    public class student
    {
        public int Id { get; set; }

        public string Name { get; set; }
         [Required]
        public string Family { get; set; }
         [Required]
        //[MaxLength(5)]
        public int Age { get; set; }


        public string Email { get; set; }

    }
}

请帮帮我

您的姓名 属性 没有 Required 属性。添加它,验证应该失败

public class student
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Family { get; set; }
    [Required]
    public int Age { get; set; }
    public string Email { get; set; }
}

我能够解决问题。

我不得不将 [required] 属性放在我的 name 属性 下面,而不是放在上面。

public class student
    {
        public int Id { get; set; }
         [Required(AllowEmptyStrings = false)]
        public string Name { get; set; }
        [Required(AllowEmptyStrings=false)]
        public string Family { get; set; }
      [Required(AllowEmptyStrings = false)]
        //[MaxLength(5)]
        public int Age { get; set; }


        public string Email { get; set; }

    }