1

我收到错误:“System.Web.Helpers.Chart”类型中不存在类型名称“Models”

请帮我解决这个问题。以下是使用 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; }
            }
  }

控制器:

        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);
                }       
            }
        }

在此处输入图像描述

查看代码:

        @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>
4

1 回答 1

6

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

@model global::Chart.Models.FooBarModel

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

例如:

namespace MyCompany.MyApplication.Models
于 2013-10-25T13:18:11.013 回答