Insight
Insight

Reputation: 196

MVC - Cannot find View after form is being send

i have the following structure in my (mvc) application

-Views
--Home
---Index.cshtml

--User
---Change.cshtml

I am using a Form like this

@using (Html.BeginForm("Change", "User", new { id = 2 }))
{
     // Code ...
}

if all textfields are filled, you can click on the submit-button. In the background, actions will be taken and the user is being taken back to the Home/Index.cshtml page.

My code for returning the 'View' looks like this

[HttpPost]
public ActionResult Change(FormCollection collection)
{
  //logic....
  return View("Index", "Home");
}

The problem starts here. I cannot switch back to Home/Index. I get an error which says, the view cannot be found. It tries to look up in the Views/User for the Index, but not in Home. Is there a reason for that?

Upvotes: 0

Views: 407

Answers (1)

Darren
Darren

Reputation: 70786

You're in the User controller so when you do the following:

return View("Index", "Home");

MVC will look for the Index page in the Users folder. Specifying Home is not telling it to look into the Home Controller. That parameter is reserved for the model.

I would not recommend routing from one Controller to another, however you could use

RedirectToAction which allows you to specify the action name and the controller, in your case:

return RedirectToAction("Change", "Home");

Upvotes: 1

Related Questions