我在這裏有一個非常大的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; }
}
}
什麼錯誤?你能顯示你的視圖代碼嗎? –
我沒有。剛刪除查看,因爲不起作用 – xerror
錯誤信息?在你看來'@model List'? –