简体   繁体   English

Razor的当前上下文中不存在名称“模型”

[英]The name 'Model' does not exist in the current context in Razor

First, I've read all the questions regarding the issue. 首先,我已经阅读了有关此问题的所有问题。

Yes, I've checked/deleted my web.config file in the folder containing the view. 是的,我已经在包含该视图的文件夹中检查/删除了我的web.config文件。

Yes, there are other other views in the same folder with no problems. 是的,同一文件夹中还有其他视图,没有问题。

Yes, there are other classes in the same namespace with my model's namespace (but regardless of the class, I'm getting this error). 是的,在与我的模型的名称空间相同的名称空间中还有其他类(但是无论该类如何,我都会收到此错误)。

Yes, I've cleaned the solution. 是的,我已经清洗了溶液。

Yes, I've restarted Visual Studio. 是的,我已经重新启动了Visual Studio。

Yes, I'm on the latest version (as of writing, 2015, Update 2) of Visual Studio. 是的,我使用的是Visual Studio的最新版本(截止至撰写,2015年,更新2)。

No, I'm not doing anything fancy about Razor pages, syntax, web.config etc. 不,我对Razor页面,语法,web.config等没有任何幻想。

No, I don't have any Visual Studio plugins. 不,我没有任何Visual Studio插件。

Whatever I try, Visual Studio keeps telling me that the Model doesn't exist in the current context only for a particular file. 无论我尝试什么,Visual Studio总是告诉我该模型仅在特定文件中不存在于当前上下文中 There are other cshtml files just as this one in the same folder, many of them have strongly typed models (from the same folder, with the same protection level), and they are all working correctly. 与此文件夹中的其他cshtml文件一样,其中许多具有强类型化的模型(来自同一文件夹,具有相同的保护级别),并且它们都正常工作。 The problem exist just at the beginning of the file: 该问题仅存在于文件的开头:

在此处输入图片说明

What is wrong? 怎么了?

@model is a reserved word for specifying the type of the model. @model是用于指定模型类型的保留字。

Use @Model to get the instance. 使用@Model获取实例。

In your case simply use another name for your variable. 在您的情况下,只需为变量使用另一个名称即可。

The solution from this article worked for me: 本文的解决方案对我有用:

Run VS as administrator 以管理员身份运行VS

Then run Uninstall-Package -Id Microsoft-Web-Helpers followed by: Install-Package -Id Microsoft.AspNet.WebHelpers 然后运行Uninstall-Package -Id Microsoft-Web-Helpers然后运行: Install-Package -Id Microsoft.AspNet.WebHelpers

While posting the question, I've figured out the error. 发布问题时,我发现了错误。 It's due to Visual Studio's poor Razor syntax parsing. 这是由于Visual Studio糟糕的Razor语法解析。

As the Model may be null, I was assigning it to a new variable and if it was null, I was creating a new instance (I can't directly assign to Model as it's read-only), and the name of the variable was model : 由于Model可能为null,因此我正在将其分配给新变量,如果它为null,则正在创建新实例(我不能直接将其分配给Model因为它是只读的),并且变量的名称为model

var model = Model;
if(model == null) {
  model = ...
}

Anyway. 无论如何。 I was using that model instance which was guaranteed to be non-null and have some values. 我使用的model实例保证为非null并具有一些值。 Below, I was using <meta name="description" content="@model.Description" /> and Razor thought that I was declaring a new model type for the page when it saw @model , while the @ was for escaping into C# and model was my variable name. 在下面,我使用的是<meta name="description" content="@model.Description" /> ,Razor认为我在看到@model时为页面声明了新的模型类型,而@是用于转义到C#中。 model是我的变量名。 I've changed the varible model to m (or anything else that wouldn't be a keyword) and the problem went away. 我将变量model更改为m (或其他任何不会作为关键字的模型),问题就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM