2016-05-17 52 views
1

我在这里有一个非常大的issuse。我试图列出所有汽车的列表并将它们推送到View,但它不起作用,因为我认为。任何想法我怎么能这样做?我会非常心存感激使项目清单C#MVC

这是我的车控制器

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Automarket.Models; 
using System.Data.Entity; 
using System.Web.Script.Serialization; 
using System.Net; 

namespace Automarket.Controllers 
{ 
    public class CarController : Controller 
    { 
     OurDBContext db = new OurDBContext(); 
     private object Viewbag; 
     private readonly object ds; 

     // GET: Automarket 
     public ActionResult Index() 
     { 

      List<Marke> marke = db.Marke.ToList(); 
      List<Modeli> modeli = db.Modeli.ToList(); 
      JavaScriptSerializer oSerializer = new JavaScriptSerializer(); 
      string deps = oSerializer.Serialize(modeli); 
      ViewData["deps"] = deps; 
      ViewData["marke"] = marke; 
      ViewData["modeli"] = modeli; 
      return View(db.car.ToList()); 
     } 

     // GET: Cars/Details/5 
     public ActionResult Details(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Car car = db.car.Find(id); 
      if (car == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(car); 
     } 

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

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create([Bind(Include = "CarID,Make,Model,Price,Registration,Mileage,FuealType,Country,ZipCode,Picture")] Car car) 
     { 
      if (ModelState.IsValid) 
      { 
       db.car.Add(car); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(car); 
     } 

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

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit([Bind(Include = "CarID,Make,Model,Price,Registration,Mileage,FuealType,Country,ZipCode,Picture")] Car car) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(car).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(car); 
     } 

     public ActionResult Delete(int? id) 
     { 
      if (id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      Car car = db.car.Find(id); 
      if (car == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(car); 
     } 

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

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


    } 
} 

这里是我的模型

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 

namespace Automarket.Models 
{ 
    public class Car 
    { 

     public int CarID { get; set; }  
     public string Make { get; set; }  
     public string Model { get; set; } 
     public float Price { get; set; } 
     public int Registration { get; set; } 
     public double Mileage { get; set; } 
     public string FuealType { get; set; } 
     public string Country { get; set; } 
     public int ZipCode { get; set; } 
     public string pathToImage { get; set; } 



    } 
} 
+0

什么错误?你能显示你的视图代码吗? –

+0

我没有。刚删除查看,因为不起作用 – xerror

+0

错误信息?在你看来'@model List '? –

回答

0

要在您的视图显示对象的列表,bascally,您需要:

在控制器中,使用View(<data>)方法发送要查看的对象列表:

public ActionResult Index() { 
    OurDBContext db = new OurDBContext(); 
    return View(db.car.ToList()); 
} 

看来,接收对象的名单在模型变量:

@model List<Car> 

<html> 
... 
<body> 
    @foreach(var item in Model) { 
     <p>@item.Property</p> 
    } 
</body></html> 
+0

没有处理谢谢男人这么多:) – xerror

+0

;-)请,如果这是有用的,接受这个答案作为解决您的问题/问题。 –