我正在开发一个 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);
}
我想以 id 作为参数调用以下 Index 方法:
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:参数字典包含方法'System.Web.Mvc.ActionResult Index(Int32)'的不可空类型'System.Int32'的参数'id'的空条目
我正在尝试的代码是:
return View("Index", id);
我可以帮忙吗?
提前致谢