Mac*_*icz 0 .net c# asp.net asp.net-mvc
我正在创建一个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);
}
Run Code Online (Sandbox Code Playgroud)
和:
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);
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是,我收到了这个错误:
参数字典包含'ClassDeclarationsThsesis.Controllers.AccountController'中方法'System.Web.Mvc.ActionResult AddGroupsQty(Int32,Int32)'的非可空类型'System.Int32'的参数'id'的空条目.可选参数必须是引用类型,可空类型,或者声明为可选参数.纳兹瓦参数:参数
我该怎么做?
你的操作方法AddGroupsQty有两个参数,qty并且id,但是,当你这样做的重定向,您的查询字符串没有ID之一!相反,它有一个叫subject_id
要修复错误,请将路线值项目更改subject_id为id
return RedirectToAction("AddGroupsQty", "Account", new { qty = model.qty, id = id});
Run Code Online (Sandbox Code Playgroud)
或者您可以将AddGroupsQty操作方法参数更改id为subject_id(确保检查所有其他位置并根据需要修复查询字符串/路由值)