我正在創建一個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
How do I go about this?