类型名称“模型”在类型“ System.Web.Helpers.Chart”中不存在

use*_*053 0 asp.net-mvc razor asp.net-mvc-4

我收到错误消息:类型'Models'在类型'System.Web.Helpers.Chart'中不存在

请帮助我解决这个问题。这是使用mvc和razor语法开发的代码:

模型

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;

  namespace Chart.Models
  {
            public class FooBarModel
            {
                public IEnumerable<SelectListItem> Locations { get; set; }
            }
  }
Run Code Online (Sandbox Code Playgroud)

控制器:

        using Chart.Models;
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;

        namespace Chart.Controllers
        {
            public class FooController : Controller
            {
                //
                // GET: /Foo/

                public ActionResult Index()
                {
                    var locations = new[]
                    {
                        new SelectListItem { Value = "US", Text = "United States" },
                        new SelectListItem { Value = "CA", Text = "Canada" },
                        new SelectListItem { Value = "MX", Text = "Mexico" },
                    };

                    var model = new FooBarModel
                    {
                        Locations = locations,
                    };

                    return View(model);
                }       
            }
        }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

查看代码:

        @model Chart.Models.FooBarModel             // intellisense shows error on this line as well

        @{
            Layout = null;
        }

        <!DOCTYPE html>

        <html>
        <head>
            <meta name="viewport" content="width=device-width" />
            <title>Index</title>
            <script>
                var locations = @Html.Raw(Json.Encode(Model.Locations));
            </script>
        </head>
        <body>
            <div>        
            </div>
        </body>
        </html>
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 5

您可以完全限定名称空间,以避免与System.Web.Helpers.Chart视图范围内的类冲突:

@model global::Chart.Models.FooBarModel
Run Code Online (Sandbox Code Playgroud)

基本上,在名称空间中使用类名是一个坏主意。例如,Chart是在System.Web.Helpers命名空间中。

例如:

namespace MyCompany.MyApplication.Models
Run Code Online (Sandbox Code Playgroud)