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 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);
}
}
}
View Code:
@model Chart.Models.FooBarModel // intellisense shows error on this line as well
@{
Layout = null;
}
Index
Answer
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
No comments:
Post a Comment