2016-12-27 82 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); 
} 

但不幸的是,我得到這個錯誤:

AddGroupsQty有2個參數

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

你的操作方法,qtyid,但是當你做重定向時,你的querystring沒有id!相反,它有一個叫subject_id

要修正這個錯誤,更改從subject_id路線值項目id

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

或者你可以在AddGroupsQty動作方法的參數從id改變subject_id(請務必檢查所有其他地方並根據需要修復查詢字符串/路由值)

相關問題