The type of namespace 'Mvc' Does not exist

2020-06-01 10:00发布

问题:

I am working on a MVC project in Visual Studio 2015 (originally created in VS 2013)

It all builds correctly, but while coding, the views show alot of errors.

@{
 ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";

string quoteType = "Fee Estimate";
if (Model.Quote.QuoteType == "QuoteType")
{
}
}

In this code that is on the top of one of my views, the Viewbag, Layout & Model is all underlined showing an error.

The errors are:

Error CS0103 The name 'Model' does not exist in the current context Quilgroup C:\,,,\Index.cshtml 268

Error CS0234 The type or namespace name 'Mvc' does not exist in the namespace 'Microsoft.AspNet' (are you missing an assembly reference?) Quilgroup C:...\Index.cshtml 1

I think it is something wrong with the dev enviornment, because it still compiles and runs correctly.

回答1:

Delete %localappdata%\Microsoft\VisualStudio\{version}\ComponentModelCache folder then restart Visual Studio.

I had the same problem and this solved it.

Source: After installing AspNet5RC1, can no longer open cshtml files in any previous / new MVC project



回答2:

This is a tooling problem. VS 2015 contains MVC tooling only for MVC version 5.x and above. You need to upgrade ASP.NET MVC to version 5+.

I other words your MVC 4.x app will still be compiled and run correctly but the development experience and editing in VS will be less optimal (a lot of error like underlines, go to view won't work, etc.)



回答3:

May be, You are missing the reference inclusion part and Model has been called instead of model.

*<!--add the refrence, you are missing the reference. this should remove those errors-->*
@model ManageQuote.Models.Quote
@{
 ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";

string quoteType = "Fee Estimate";
*<!-- use model instead of Model,I think its what you should be calling here, it would be easy if model classes were known -->*
if (model.Quote.QuoteType == "QuoteType"){
   }
}


回答4:

Open your project folder search for *.suo files delete all these files

open .sln file in notepad or notepad++ then check for the assembly paths, are they mapped correctly? if not then map it correctly.

Then clear all your temporary files.

Clean your code. Rebuild. This will solve your problem.