制作项目列表C#MVC

时间:2016-05-17 21:27:51

标签: c# asp.net-mvc

我这里有一个非常大的问题。我正在尝试列出所有车辆的列表并将它们推送到View,但它不能像我想象的那样工作。不知道怎么办?我会非常感谢

这是我的汽车控制器

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



    }
}

1 个答案:

答案 0 :(得分:0)

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

在Controller中,使用View(<data>)方法将对象列表发送到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>