2017-08-25 90 views
0

我有一個簡單的MVC代碼,其中包含validation.but對名稱屬性的驗證(必需)不起作用。在「InsertStudent」操作結果後,我在控制器中創建BreakPoint。爲什麼mvc驗證在我的代碼中不起作用?

所以我運行並填寫表單,而不填寫名稱文本框,我期望在我的控制器中的「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; } 

    } 
} 

請幫我

+1

您的「名稱」屬性不是必需的。你的「家庭」財產是。這是一個錯字? –

+0

是的,這是正確的 – bami

+0

您應該更新您的示例和您的代碼,以在Name屬性中添加必需的屬性。讓我們知道驗證是否仍然失敗。 – Wndrr

回答

1

你不必一個您的名稱屬性上的屬性。添加它,它的驗證應該失敗

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; } 
} 
+0

你不需要'AllowEmptyStrings = false'(默認是'false') –

+0

請不要浪費領帶回答將被關閉的題外問題 –

+0

wndrr是它失敗了。你的意思是我不能要求名稱屬性的屬性無論如何?!! – bami

1

我能解決這個問題。

我不得不把[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; } 

    } 
+0

請記住,元數據(有關屬性/方法或字面上任何事情的信息)將始終在事物之前它描述:-) – Wndrr

+0

注意:正如@Stephen Muecke所指出的那樣,'AllowEmptyStrings = false'是多餘的,因爲'false'是默認值。 – Wndrr

相關問題