【问题标题】:View engine can't find my view视图引擎找不到我的视图
【发布时间】:2012-10-06 10:57:35
【问题描述】:

尝试从发布操作返回 ViewResult 时出现以下错误:

未找到视图“索引”或其主视图,或者没有视图引擎支持搜索到的位置。搜索了以下位置: ~/Views/Home/Index.cshtml ~/Views/Shared/Index.cshtml ~/Views/Home/Home.cshtml
~/Views/Shared/Home.cshtml ~/Views/Home/Index.aspx
~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx ~/Views/Home/Home.master
~/Views/Shared/Home.master ~/Views/Home/Home.vbhtml
~/Views/Shared/Home.vbhtml

我的观点肯定得到认可,因为它适用于 GET 操作。

POST动作中返回ViewResult的代码是:

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

Here is the view.

谁能提出为什么这不起作用?

更多上下文: get 操作可以很好地显示视图。 post 操作实际上是针对不同的 url 但返回相同的视图。这是导致问题的发布操作。 GET 和 POST 操作都在同一个控制器 HomeController 上。

这是(精简的)控制器:

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        return View(new LoginModelBase());
    }

    [HttpPost]
    public ActionResult Login(UsernameLoginModel Model)
    {
        ...
        return View("Index", "Home", Model);
    }
}

【问题讨论】:

  • 你能再贴一些代码吗.....
  • 两个 url 是否指向同一路径?
  • 您在项目中使用 RazorGenerator 吗?
  • @KirillBestemyanov Razor 生成器被引用的项目使用。 _layout 来自所述项目。此视图未使用剃须刀生成器。
  • @AmiramKorach。 GET url 是mydomain.com 或mydomain.com/Home/Index。 POST 网址是mydomain.com/Home/Login

标签: c# asp.net asp.net-mvc asp.net-mvc-4 viewengine


【解决方案1】:

我刚刚意识到它是什么!我使用了View(...) 方法的错误重载。

应该是:

View("Index", Model);

【讨论】:

  • 是的,“Home”指的是 Master,而不是 Controller。
猜你喜欢
  • 2017-10-15
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
  • 2013-06-23
  • 2013-08-18
  • 1970-01-01
相关资源
最近更新 更多