类型名称“模型"在类型"System.Web.Helpers.Chart"中不存在 [英] The type name 'Models' does not exist in the type 'System.Web.Helpers.Chart'

查看:101
本文介绍了类型名称“模型"在类型"System.Web.Helpers.Chart"中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting error : The type name 'Models' does not exist in the type 'System.Web.Helpers.Chart'

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

Please help me in resolving this. Here is the code developed with mvc and razor syntax:

型号

  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>

推荐答案

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

You could fully qualify the namespace to avoid the collision with the System.Web.Helpers.Chart class which is in scope in the view:

@model global::Chart.Models.FooBarModel

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

Basically it's a bad idea to use class names in your namespaces. For example Chart is a class defined in the System.Web.Helpers namespace.

例如:

namespace MyCompany.MyApplication.Models

这篇关于类型名称“模型"在类型"System.Web.Helpers.Chart"中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆