列出项目C#MVC

列出项目C#MVC,c#,asp.net-mvc,C#,Asp.net 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.

我这里有一个很大的问题。我试着列出所有的车,并把它们推到视图中,但这并不像我想象的那样有效。你知道我该怎么做吗??我会非常感激的

这是我的汽车控制器

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);
        }


    }
}

要在视图中显示对象列表,基本上,您需要:

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

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>
@型号列表
...
@foreach(模型中的var项目){
@项目.财产

}
要在视图中显示对象列表,基本上需要:

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

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>
@型号列表
...
@foreach(模型中的var项目){
@项目.财产

}
错误是什么?你能显示你的视图代码吗?我没有。刚刚删除了视图,因为没有工作错误消息?在您放置的视图中,
@model List
?我只添加了在控制器中显示汽车列表的方法:public ActionResult DisplayCars(){List Cars=new List();Car Car Car=new Car();db.Car.add(Car);return view();}在视图中,我在这里得到了错误消息:在App_Web_kkbg42sx.dll中发生了类型为“System.NullReferenceException”的异常,但未在user code@foreach(模型中的var项)中处理{错误是什么?你能显示你的视图代码吗?我没有。只是删除了视图,因为它不起作用错误消息?在你放置的视图中
@model List
?我只是添加了在控制器中显示汽车列表的方法:public ActionResult DisplayCars(){List Cars=new List();Car Car Car=new Car();db.car.Add(car);return View();}在视图中,我在这里得到了错误消息:在App_Web_kkbg42sx.dll中发生了类型为“System.NullReferenceException”的异常,但未在用户代码@foreach中处理(模型中的var项)(非常感谢男人:);-)如果这有用,请接受这个答案作为解决你的问题。非常感谢男人:);-)如果这有用,请接受这个答案作为解决你的问题。