2017-05-01 13 views
-1

我創建了一個數據庫與實體框架,我創建了第一個實體作爲carlot和第二輛車,將在各自的地段列出汽車。我一直在想的東西是錯誤與我的模型......這裏下面的模型從asp.net模型中的控制器中的CS0118錯誤

namespace CarLot.Models 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Data.Entity; 
    using System.Linq; 

    public class CarModel : DbContext 
    { 
public CarModel() 
      : base("name=CarModel") 
     { 
     } 

     // Add a DbSet for each entity type that you want to include in your model. For more information 
     // on configuring and using a Code First model, see http://go.microsoft.com/fwlink/?LinkId=390109. 

     public DbSet<CarLot> CarLots { get; set; } 
     public DbSet<Car> Cars { get; set; } 
    } 

    public class CarLot 
    { 
     //Primary Key 
     public int CarLotID { get; set; } 
     public string Name { get; set; } 

     public string Section { get; set; } 

     public virtual ICollection<Car> Cars { get; set; } 
    } 

    public class Car 
    { 
     public int CarId { get; set; } 

     public string Make { get; set; } 

     public string Model { get; set; } 

     public int Year { get; set; } 

     public int Price { get; set; } 

     public bool isNew { get; set; } 


     //foreign key 
     public int CarLotID { get; set; } 
     public virtual CarLot Carlot { get; set; } 
    } 


} 

我繼續支架材料汽車控制器,不顯示任何錯誤。 但不是我的carlot,這是下面的CarLot文件...它給我CarLot CS0118錯誤,如下面評論,6 CarLot給我CS0118,其中指出「CarLot是命名空間,但用作類型」

sing System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Entity; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.Mvc; 
using CarLot.Models; 

namespace CarLot.Controllers 
{ 
    public class CarLotsController : Controller 
    { 
     private CarModel db = new CarModel(); 

     // GET: CarLots 
     public ActionResult Index() 
     { 
      return View(db.CarLots.ToList()); 
     } 

     // GET: CarLots/Details/5 
     public ActionResult Details(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      // It is giving me the CS0118 error with the following CarLot 

      CarLot carLot = db.CarLots.Find(id); 
      if (carLot == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(carLot); 
     } 

     // GET: CarLots/Create 
     public ActionResult Create() 
     { 
      return View(); 
     } 

     // POST: CarLots/Create 
     // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
     // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create([Bind(Include = "CarLotID,Name,Section")] CarLot carLot) 
     { 
      if (ModelState.IsValid) 
      { 
       db.CarLots.Add(carLot); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(carLot); 
     } 

     // GET: CarLots/Edit/5 
     public ActionResult Edit(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      CarLot carLot = db.CarLots.Find(id); 
      if (carLot == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(carLot); 
     } 

     // POST: CarLots/Edit/5 
     // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
     // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit([Bind(Include = "CarLotID,Name,Section")] CarLot carLot) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(carLot).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(carLot); 
     } 

     // GET: CarLots/Delete/5 
     public ActionResult Delete(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      CarLot carLot = db.CarLots.Find(id); 
      if (carLot == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(carLot); 
     } 

     // POST: CarLots/Delete/5 
     [HttpPost, ActionName("Delete")] 
     [ValidateAntiForgeryToken] 
     public ActionResult DeleteConfirmed(int id) 
     { 
      CarLot carLot = db.CarLots.Find(id); 
      db.CarLots.Remove(carLot); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing) 
      { 
       db.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 
    } 
} 
+3

既您的命名空間和類名是卡洛特不這樣做 – Sachu

回答

0
namespace CarLot.Models 
{ 


    public class CarLot 
    { 

問題涉及這些代碼都命名空間和類名是卡洛特 嘗試改變其中的一個

+0

感謝,通過去除錯誤改變班級名稱,謝謝! –

+0

很高興它幫助..請將它標記爲已回答 – Sachu

相關問題