Question

I am developing an MVC 4 C# application and am after an answer on how to load a different view with a parameter from a function in the same controller.

Here is my code:

[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);
}

I am wanting to call the following Index method, with the id as a parameter:

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);
}

Here is the error that I am getting:

[ArgumentException: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)'

The code I am trying is at line:

return View("Index", id);

Can I please have some help with this?

Thanks in advance

Was it helpful?

Solution

return RedirectToAction("Index", new{id=yourId}); is what you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top