The type name 'Models' does not exist in t

2019-09-16 03:31发布

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

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

Model

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

Controller:

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

enter image description here

View Code:

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

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-16 04:05

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

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.

For example:

namespace MyCompany.MyApplication.Models
查看更多
登录 后发表回答