2017-01-04 4 views
1

私はMVCアプリケーションを作成しています。私はこのような事前定義されたパラメータで、私のビューを開きます。MVCデータがビューに渡されます

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

そして、これが正常に動作し、データがsubject_idqtyが正しく渡されます。しかし、私のビューPickGroupForHomeworkには、記入する書式が含まれており、それが検証されます。入力が有効でない場合、ウィンドウは単に前のビューで定義したようにsubject_idqtyというデータでリロードするだけです。私はこのようにこうします:

public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model) 
     { 
      ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2(); 
      model.groups = entities.Groups.ToList(); 
      model.users = entities.Users.ToList(); 
      int id = model.subject_id; 
      var subj = entities.Subjects 
        .Where(b => b.class_id == model.subject_id) 
        .FirstOrDefault(); 
      if (subj != null) 
      { 
       model.subject_name = subj.name; 
      } 
      if (ModelState.IsValid) 
      { 

      } 
      else 
      { 
       return View(model); 
      } 
      return View(model); 

     } 

しかし、結果のURLには必要なデータは含まれていませんが、単純な見方があります。私はそれをどうやって行うのですか?あなたのためのオーダーWEPアプリは1ビューのためにあなたのモデルを設定すると、実際に他の投稿やデータを保存するための作業を行うには、次の2つの操作が必要になり、仕事をするには

+0

なぜドンあなたは控えめな検証をしません。これはほとんどの問題がクライアント側にキャッチされていることを意味します – Liam

+0

ビューも表示できますか? – user7351608

+0

@KwekuReginaldWade編集を参照してください – jjj21

答えて

0

public ActionResult PickGroupForHomework(int subject_id, int qty) 
    { 
     //Initialize your model here. Below is just an example. 
     ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2(); 

     PickGroupForHomeworkViewModel model = new PickGroupForHomeworkViewModel(); 

     model.groups = entities.Groups.ToList(); 
     model.users = entities.Users.ToList(); 

     model.subject_id = subject_id; 
     model.qty = qty; 

     return View("PickGroupForHomework", model); 
    } 

    [HttpPost] 
    public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model) 
    { 
     ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2(); 

     int id = model.subject_id; 

     var subj = entities.Subjects 
       .Where(b => b.class_id == model.subject_id) 
       .FirstOrDefault(); 

     if (subj != null) 
     { 
      model.subject_name = subj.name; 
     } 
     if (ModelState.IsValid) 
     { 
      //Save to database 
      [code goes here] 

      //return to a View to show your results 
      return View("[Your view to see the results]") 
     } 

     //Model Validation did not pass 
     //or exception occurred go back to View 
     return View(model); 
    } 
+1

これで問題は解決しません – jjj21

+0

デバッグ時にどのようなコードが使われますか? – user7351608

+1

別のウィンドウから移動すると、 'http:// localhost:3449/Account/PickGroupForHomework?subject_id = 2&qty = 4'ですが、間違った検証の後に' http:// localhost:3449/Account/PickGroupForHomework' – jjj21

関連する問題