2016-12-27 6 views
0

MVCアプリケーションを作成しています。パラメータ辞書にパラメータ 'id'のヌルエントリが含まれています

public ActionResult AddGroup(AddGroupViewModel model) 
     { 
      var entities = new ClassDeclarationsDBEntities1(); 
      var model1 = new AddGroupViewModel(); 
      model1.Subjects = entities.Subjects.ToList(); 
      model1.Users = entities.Users.ToList(); 
      if (ModelState.IsValid) 
      { 
       var subj = entities.Subjects 
        .Where(b => b.name == model.subject_name) 
        .FirstOrDefault(); 
       int id = subj.class_id; 
       return RedirectToAction("AddGroupsQty", "Account", new { qty = model.qty, subject_id = id}); 
      } 
      return View(model1); 
     } 

そして:私はこのようなビューの間でいくつかのデータを渡しています

public ActionResult AddGroupsQty(int qty, int id) 
{ 
    ClassDeclarationsDBEntities1 entities = new ClassDeclarationsDBEntities1(); 

    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; 
    return View(model); 
} 

しかし残念ながら、私はこのエラーを取得:2つのパラメータ、qtyを持ってAddGroupsQty

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddGroupsQty(Int32, Int32)' in 'ClassDeclarationsThsesis.Controllers.AccountController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Nazwa parametru: parameters
enter image description here How do I go about this?

答えて

6

あなたのアクションメソッドをidしかし、リダイレクトを実行すると、クエリ文字列にはIDが1つもありません!その代わりに、エラーを修正id

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

subject_idからあなたのルートの値項目を変更するには

subject_idを1と呼ばれたか、あなたはすべて確認してください(subject_ididからAddGroupsQtyアクションメソッドのパラメータを変更することができます他の場所に移動し、必要に応じてクエリーストリング/ルート値を修正してください)

関連する問題