MVC 链接到视图

MVC Linking to views

MVC 的新手所以学习(以一种令人沮丧的方式,因为在 webforms 中找到过去需要 5 分钟的简单事情变得过于复杂)

所以我在 VS2015 中设置了一个解决方案,其中包含先决条件帐户部分。

我还在数据库中设置了表并从中构建了实体(生成一些 CRUD

这样我就可以登录并进入我的 AccountHome 视图(return View("AccountHome")AccountController 中)

从我的 AccountHome 页面,我可以使用单独的控制器导航到另一个视图
(@Html.ActionLink("Add Foods", "Create", "fad_userFoods", new { id = ViewBag.userID }, new { @class = "btn btn-default" }))

这里我做了一些表单输入,数据已成功添加到数据库中,但这就是我的问题所在。输入数据后,我想返回 AccountHome 视图

所以在我的 fad_userFoodsControler 我有这个代码
(return RedirectToAction("AccountHome","Account"))
- 说找不到资源!!!

在我的 AccountController 中,我有以下代码:

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

在我的AccountViewModels中我有这个代码:

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

最后,在我的 AccountHome.cshtml 页面中,我在顶部声明了这个 (@model FiveADayMVC2.Models.AccountHomeViewModel)

我哪里错了?看了很多帖子,都是这样的。我知道该视图存在,因为我可以通过登录访问它。然而,如果我手动输入 url(即我的 url 后跟 /Account/AccountHome),它也找不到???

这将是我缺乏理解,但在哪里?

提前感谢您的指点

尝试使用 RedirectToAction 这会将用户导航到给定控制器中特定操作的视图。

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

更新:

确保你有 HttpGet 的控制器

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

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

由于您将 [HttpPost] 添加到控制器,它将是在 "AccountHome"

上创建 post 之后执行的那个