MVC3 的当前上下文中不存在名称“模型" [英] The name 'model' does not exist in current context in MVC3

查看:18
本文介绍了MVC3 的当前上下文中不存在名称“模型"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中添加了一个 cshtml 页面.当我尝试向其中添加以下声明时,出现错误:当前上下文中不存在名称‘模型’".

I added a cshtml page in an project. When I tried to add the following declaration to it, I get an error: "The name 'model' does not exist in current context".

@model xyz.abc.SomeClass

我检查了参考资料,所有资料都已到位.我在视图文件夹中添加了一个 web.config,但这并没有修复它.

I checked the references, all of them are in place. I added a web.config in view folder, but that didn't fix it.

有什么我遗漏的吗?

推荐答案

更新:如果您使用的是较新版本的 MVC,同样的过程也适用,只需确保在 Web 中使用正确的版本号.config 的 行.

好吧,我发现自己遇到了和你一样的事情,经过进一步研究,我发现了问题所在!

Well, I found myself experiencing the same thing you did, and after a bit further research, I found out what the problem is!

您需要为 Views 文件夹包含默认的 MVC3 web.config.MVC3 有两个:一个在您的应用程序的根目录中,一个在 views 文件夹中.这有一个包含命名空间的部分.确保你的看起来像这样:

You need to include the default MVC3 web.config for the Views folder. MVC3 has two: one in the root for your application, and one for the views folder. This has a section for included namespaces. Be sure that yours looks something like this:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

我建议你创建一个新的MVC3项目,然后把为你创建的web.config复制到你的views文件夹中.

I suggest that you create a new MVC3 project, then just copy the web.config created for you into your views folder.

重要完成此操作后,您需要关闭文件并重新打开它.瞧!智能感知!

Important Once you've done that, you need to close the file and reopen it. Voila! Intellisense!

这篇关于MVC3 的当前上下文中不存在名称“模型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆