2016-12-28 5 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); 
     } 

のViewModel:

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

そして、問題はビューがある時はいつでもということですウィンドウ内のフォームの検証は常に有効です。ビューは次のようになります。私は、コードのこの部分についての何が間違っている何のアイデアを持っていませんが、いつでも、私はこのウィンドウを開く

@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は両方ともnullです。どうして? @BalajiM –

答えて

0

モデルバインダーに基づいて検証が実行されます。そこでコントローラのアクションは、パラメータとしてビューモデルでなければなりません。

Pubic ActionResult AddGroupsQty(AddGroupsQtyViewModel value) 
+0

フォームを投稿してモデル検証をしたいときは、@Balajiが指定したようなアクションメソッド引数として適切なビューモデルを渡す必要があります。このアプローチを使用して、コードに関連する変更を加えてみてください。 –

関連する問題