2016-05-17 82 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

;-)請,如果這是有用的,接受這個答案作爲解決您的問題/問題。 –