简体   繁体   English

ASP.NET MVC3中的编译错误CS0103

[英]Compilation Error CS0103 in ASP.NET MVC3

I'm working on a project with ASP.NET and SQL Server 2012, and when I try to load the project it says: 我正在使用ASP.NET和SQL Server 2012开发一个项目,当我尝试加载该项目时,它说:

Compilation Error CS0103: The name 'model' does not exist in the current context 编译错误CS0103:名称“模型”在当前上下文中不存在

This is my code: 这是我的代码:

@model IEnumerable<SegundoParcialP3.Models.Productos>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using(Html.BeginForm())
{}
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Nombre)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Precio)
        </th>
        <th></th>
    </tr>

@foreach (var item in model) {
    <tr> 
        <td>
            @Html.DisplayFor(modelItem => item.Nombre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Precio)
        </td>
    </tr>
}
    <tr> 
        <td>
            @Html.ActionLink("Edit", "Edit") |@*, new { id=item.Id }*@
            @Html.ActionLink("Details", "Details") |@*, new { id=item.Id }*@
            @Html.ActionLink("Delete", "Delete") | @*, new { id=item.Id }*@
        </td>
    </tr>


</table>

Your problem lies here: 您的问题出在这里:

@foreach (var item in model) {

change it to: 更改为:

@foreach (var item in Model) {

lowercase model is not anything, uppercase is a member of the ViewPage class. 小写模型不是什么,大写是ViewPage类的成员。

As a side note, it would have been easier to find this if you hadn't used the word "model" virtually everywhere... When you have these problems, start renaming things when you can. 附带说明一下,如果您几乎没有在所有地方都使用“模型”一词,则查找起来会更容易...当遇到这些问题时,请尽可能重命名。 That can help narrow it down. 这可以帮助缩小范围。

I had the same issue when I created Views folder manually. 手动创建Views文件夹时遇到相同的问题。 Fix: I created a Web.config file under Views folder and added the configuration details. 修复:我在“视图”文件夹下创建了一个Web.config文件,并添加了配置详细信息。 Please see below. 请看下面。


<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

    <!--
        Enabling request validation in view pages would cause validation to occur
        after the input has already been processed by the controller. By default
        MVC performs request validation before a controller processes the input.
        To change this behavior apply the ValidateInputAttribute to a
        controller or action.
    -->
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

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

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