我收到错误:“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>
答案 0 :(得分:4)
您可以完全限定命名空间以避免与视图中范围内的System.Web.Helpers.Chart
类发生冲突:
@model global::Chart.Models.FooBarModel
基本上,在命名空间中使用类名是个坏主意。例如,Chart
是System.Web.Helpers
命名空间中定义的类。
例如:
namespace MyCompany.MyApplication.Models