stumcc
stumcc

Reputation: 185

MVC Linking to views

New to MVC so learning (in a frustrated kind of way as finding simple things that used to take 5 minutes in webforms have become over complicated)

So I've set up a solution in VS2015 which comes with the pre-requisite Account section.

I've also set up tables in a database and built the entities from that (producing some CRUD)

So I can log in and get to my AccountHome view (return View("AccountHome") in the AccountController)

From my AccountHome page, I can navigate to another view using a seperate controller
(@Html.ActionLink("Add Foods", "Create", "fad_userFoods", new { id = ViewBag.userID }, new { @class = "btn btn-default" }))

Here I do some form entry and the data is successfully added to the database but here is where my problem lies. After data entry I want to go back to the AccountHome view

So in my fad_userFoodsControler I have this code
(return RedirectToAction("AccountHome","Account"))
- says resource cannot be found!!!

In my AccountController I have the following code:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AccountHome()
{
    return View();
}

In my AccountViewModels I have this code:

public class AccountHomeViewModel
{
    public string Email { get; set; }
}

Finally, in my AccountHome.cshtml page I have this declared at the top (@model FiveADayMVC2.Models.AccountHomeViewModel)

Where am I going wrong? Looked at numerous posts and they all do it this way. I know the view exists because I can get to it from login. Yet, if I type the url manually (i.e. my url followed by /Account/AccountHome) it's not found either???

It's going to be a lack of understanding on my part but where?

Thanks in advance for any pointers

Upvotes: 0

Views: 32

Answers (1)

John Ephraim Tugado
John Ephraim Tugado

Reputation: 765

Try using RedirectToAction This will navigate the user to the view of a specific action in a given controller.

if(actionSuccess)
{
    return RedirectToAction("Index", "Home");
}

UPDATE:

Make sure you have a controller for HttpGet

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AccountHome()
{
    return View();
}

public ActionResult AccountHome()
{
    return View();
}

Since you added [HttpPost] to the controller it will be the one executed after a post was made on "AccountHome"

Upvotes: 1

Related Questions