我正在开发一个MVC 4 C#应用程序,并且回答了如何使用同一控制器中的函数加载不同视图的参数。
这是我的代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(int id, Room room)
{
if (ModelState.IsValid)
{
House houseToAddRoomsTo = db.Houses.Where(h => h.id == id).FirstOrDefault();
houseToAddRoomsTo.Rooms.Add(room);
db.SaveChanges();
return View("Index", id);
}
return View(room);
}
我想调用以下Index方法,id为参数:
public ActionResult Index(int id)
{
RoomsViewModel roomsViewModel = new RoomsViewModel();
roomsViewModel.HouseID = id;
roomsViewModel.Rooms = db.Houses.Where(h => h.id == id).First().Rooms.ToList();
return View(roomsViewModel);
}
以下是我收到的错误:
[ArgumentException:参数字典包含参数' id'的空条目。非可空类型的System.Int32' for method' System.Web.Mvc.ActionResult Index(Int32)'
我正在尝试的代码是:
return View("Index", id);
我可以请一点帮忙吗?
提前致谢
答案 0 :(得分:0)
return RedirectToAction("Index", new{id=yourId});
就是您所需要的。