我正在创建一个MVC应用程序。我在这样的视图之间传递一些数据:
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);
}
但不幸的是,我收到了这个错误:
参数字典包含参数'id'的空条目 方法的非可空类型'System.Int32' 'System.Web.Mvc.ActionResult AddGroupsQty(Int32,Int32)'in 'ClassDeclarationsThsesis.Controllers.AccountController'。可选的 参数必须是引用类型,可空类型或声明为 一个可选参数。 Nazwa参数:参数
我该怎么做?
答案 0 :(得分:6)
您的操作方法AddGroupsQty
有2个参数qty
和id
,但是当您执行重定向时,您的查询字符串没有id!相反,它有一个叫subject_id
要修正错误,请将路线值项目从subject_id
更改为id
return RedirectToAction("AddGroupsQty", "Account", new { qty = model.qty, id = id});
或者您可以将AddGroupsQty
操作方法参数从id
更改为subject_id
(确保检查所有其他位置并根据需要修复查询字符串/路由值)