2016-12-28 103 views
-1

我正在創建一個MVC應用程序。我在控制器打開一看是這樣的:MVC視圖錯誤驗證

return RedirectToAction("AddGroupsQty", "Account", new { qty = model.qty, id = id }); 

的AddGroupsQty控制器看起來是這樣的:

public ActionResult AddGroupsQty(int qty, int id) 
     { 

      var model = new AddGroupsQtyViewModel(); 
      model.subject_id = id; 
      model.qty = qty; 
      ClassDeclarationsDBEntities1 entities1=new ClassDeclarationsDBEntities1(); 
      var subj = entities1.Subjects 
        .Where(b => b.class_id == model.subject_id) 
        .FirstOrDefault(); 
      model.subject_name = subj.name; 
      if (ModelState.IsValid) 
      { 
       int maxId = 0; 
       int total = 0; 
       total = entities1.Groups.Count(); 
       if (total == 0) 
       { 
        maxId = 0; 
       } 
       else 
       { 
        maxId = entities1.Groups.Max(u => u.group_id); 
       } 
       for (int i = 0; i < qty; i++) 
       { 
        var teacher = entities1.Users 
        .Where(b => b.email.Replace(" ", String.Empty) == model.teacher_emails[i].Replace(" ", String.Empty)) 
        .FirstOrDefault(); 
        var group=new Models.Group(id, maxId+1, model.group_names[i], teacher.user_id); 
       } 
       return RedirectToAction("OperationSuccess", "Account"); 
      } 
      return View(model); 
     } 

視圖模型:

public class AddGroupsQtyViewModel 
    { 
     public int qty { get; set; } 
     public int subject_id { get; set; } 
     public string subject_name { get; set; } 
     [Required] 
     [Display(Name = "Name of group")] 
     public List<string> group_names { get; set; } 
     [Required] 
     [Display(Name = "Email of teacher")] 
     [RegularExpression(@"^[a-zA-Z0-9._%+-]+(@student.mini.pw.edu.pl|@mini.pw.edu.pl)$", ErrorMessage = "Registration limited to university domain email.")] 
     public List<string> teacher_emails { get; set; } 
    } 

而問題是,每當觀點稱爲,窗口中窗體的驗證始終爲Valid。視圖看起來是這樣的:

@using System.IdentityModel.Configuration 
@using System.Web.UI.WebControls 
@model ClassDeclarationsThsesis.Models.AddGroupsQtyViewModel 

@{ 
    ViewBag.Title = "AddGroupsQty"; 
} 

<h2>Add Groups to @Model.subject_name</h2> 
@if (Model != null) 
{ 
    using (Html.BeginForm("AddGroupsQty", "Account", new { qty = Model.qty, Model.subject_id }, FormMethod.Post, new { @class = "form-horizontal", role = "form", })) 
    { 
      @Html.AntiForgeryToken() 
      <h4>Insert data</h4> 
      <hr /> 
      <table> 
       <tr> 
        <th> 
         @Html.ValidationSummary("", new { @class = "text-danger" }) 
         <div class="form-group"> 
          @Html.LabelFor(m => m.group_names, new { @class = "col-md-2 control-label" }) 
         </div> 
        </th> 
        <th> 
         @Html.ValidationSummary("", new { @class = "text-danger" }) 
         <div class="form-group"> 
          @Html.LabelFor(m => m.teacher_emails, new { @class = "col-md-2 control-label" }) 
         </div> 
        </th> 
       </tr> 

       @for (int i = 0; i < Model.qty; i++) 
       { 
        <tr> 
         <th> 
          <div class="form-group"> 
           <div class="col-md-10"> 

            @Html.TextBoxFor(m => m.group_names[i], new { @class = "form-control" }) 
           </div> 
          </div> 
          </th> 
         <th> 
          <div class="form-group"> 
           <div class="col-md-10"> 

            @Html.TextBoxFor(m => m.teacher_emails[i], new { @class = "form-control" }) 
           </div> 
          </div> 
         </th> 
        </tr> 
       } 
      </table> 
      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type="submit" class="btn btn-default" value="Submit" /> 
       </div> 
      </div> 
        } 
         } 

我不知道什麼是錯的這一段代碼,但只要我打開這個窗口,它馬上有效,然後在控制器出現錯誤,因爲它進入IF ,但它應該首先驗證表單。

+1

陰毛的ActionResult AddGroupsQty(AddGroupsQtyViewModel值)嘗試這樣 –

+0

好吧,這工作,謝謝!但是,現在填寫表單並提交後,我的名單teacher_emails和group_names都爲空。爲什麼? @BalajiM –

回答

0

驗證基於模型聯編程序執行。所以在控制器動作應該是視圖模型作爲參數。

Pubic ActionResult AddGroupsQty(AddGroupsQtyViewModel value) 
+0

當您發佈表單並希望進行模型驗證時,必須將適當的視圖模型作爲@Balaji指定的操作方法參數傳遞。嘗試使用此方法並在您的代碼中進行相關更改。 –