【问题标题】:why mvc validation does not work in my code?为什么 mvc 验证在我的代码中不起作用?
【发布时间】:2018-02-03 07:55:08
【问题描述】:

我有一个简单的 MVC 代码,其中包含一个带有验证的表单。但是我对 name 属性的验证(必需)不起作用。我在“InsertStudent”操作结果之后在我的控制器中创建了 BreakPoint。

所以我运行它并填写表单而不填写名称文本框,我希望我的控制器中的“IsValid”为假。但这是真的!!!

我的看法是:

 @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; }

    }
}

请帮帮我

【问题讨论】:

  • 您的“姓名”属性不是必需的。您的“家庭”财产是。打错字了?
  • 是的,没错
  • 您应该更新您的示例和代码以在 Name 属性上添加所需的属性。如果验证仍然没有失败,请告诉我们。

标签: asp.net-mvc validation asp.net-mvc-4


【解决方案1】:

您的 Name 属性没有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; }
}

【讨论】:

  • 你不需要AllowEmptyStrings = false(默认是false
  • 请不要浪费领带回答离题的问题,这些问题将被关闭
  • wndrr 是的,它失败了。你的意思是我不能在 name 属性上拥有必需的属性?!!
  • 是的,您确实需要 [Required] 属性。如果您不添加它,则只需要不可为空的类型(字符串可以为空,因此默认情况下它被视为可选)
【解决方案2】:

我能够解决问题。

我必须将 [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; }

    } 

【讨论】:

  • 请记住,元数据(关于属性/方法或任何字面意思的信息)总是出现在它所描述的东西之前:-)
  • 注意:正如@Stephen Muecke 所指出的,AllowEmptyStrings = false 是多余的,因为false 是默认值。
猜你喜欢
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-23
  • 2013-08-06
相关资源
最近更新 更多