【问题标题】:Loading a different view with a parameter使用参数加载不同的视图
【发布时间】:2014-02-09 06:34:22
【问题描述】:

我正在开发一个 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:参数字典包含方法“System.Web.Mvc.ActionResult Index(Int32)”的不可空类型“System.Int32”的参数“id”的空条目

我正在尝试的代码是:

return View("Index", id);

我可以帮忙吗?

提前致谢

【问题讨论】:

    标签: c# asp.net-mvc-4 visual-studio-2012 parameter-passing asp.net-mvc-views


    【解决方案1】:

    return RedirectToAction("Index", new{id=yourId}); 是您所需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多