MVC 4模型没有填充下拉列表

时间:2015-07-24 13:52:00

标签: asp.net-mvc asp.net-mvc-4

我是MVC的新手,有三个相关的类应该在我生成控制器之后填充下拉列表,但它们是空的。课程是:

public class Vehicle
{
    public int id { get; set; }
    public string Type { get; set; }
    public int Year { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public string Tag { get; set; }
    public string Operator { get; set; }
    public int YardId { get; set; }
    public virtual Yard Yard { get; set; }
    public int StatusId { get; set; }
    public virtual Status Status { get; set; }
}

public class Yard
{
    public int YardId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Vehicle> Vehicles { get; set; }
}

public class Status
{
    public int StatusId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Vehicle> Vehicles { get; set; }
}

每辆车都有一个码头,并且会从人口下拉列表中选择一个状态。在为车辆,场地和状态生成控制器后,我添加了状态记录和场地记录,但它们没有显示车辆的下拉列表。下拉列表仍然是空的,我不确定为什么。我尝试了一个类似的模型,它生成没有问题,但我似乎在这里遗漏了一些东西。

来自VehicleController的代码位于

之下
public class VehicleController : Controller   
{
        private VehicleContext db = new VehicleContext();

    //
    // GET: /Vehicle/

    public ActionResult Index()
    {
        var vehicles = db.Vehicles.Include(v => v.Yard).Include(v => v.Status);
        return View(vehicles.ToList());
    }

    //
    // GET: /Vehicle/Details/5

    public ActionResult Details(int id = 0)
    {
        Vehicle vehicle = db.Vehicles.Find(id);
        if (vehicle == null)
        {
            return HttpNotFound();
        }
        return View(vehicle);
    }

    //
    // GET: /Vehicle/Create

    public ActionResult Create()
    {
        ViewBag.YardId = new SelectList(db.Yards, "YardId", "Name");
        ViewBag.StatusId = new SelectList(db.Status, "StatusId", "Name");
        return View();
    }

    //
    // POST: /Vehicle/Create

    [HttpPost]
    public ActionResult Create(Vehicle vehicle)
    {
        if (ModelState.IsValid)
        {
            db.Vehicles.Add(vehicle);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.YardId = new SelectList(db.Yards, "YardId", "Name", vehicle.YardId);
        ViewBag.StatusId = new SelectList(db.Status, "StatusId", "Name", vehicle.StatusId);
        return View(vehicle);
    }

    //
    // GET: /Vehicle/Edit/5

    public ActionResult Edit(int id = 0)
    {
        Vehicle vehicle = db.Vehicles.Find(id);
        if (vehicle == null)
        {
            return HttpNotFound();
        }
        ViewBag.YardId = new SelectList(db.Yards, "YardId", "Name", vehicle.YardId);
        ViewBag.StatusId = new SelectList(db.Status, "StatusId", "Name", vehicle.StatusId);
        return View(vehicle);
    }

    //
    // POST: /Vehicle/Edit/5

    [HttpPost]
    public ActionResult Edit(Vehicle vehicle)
    {
        if (ModelState.IsValid)
        {
            db.Entry(vehicle).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.YardId = new SelectList(db.Yards, "YardId", "Name", vehicle.YardId);
        ViewBag.StatusId = new SelectList(db.Status, "StatusId", "Name", vehicle.StatusId);
        return View(vehicle);
    }

    //
    // GET: /Vehicle/Delete/5

    public ActionResult Delete(int id = 0)
    {
        Vehicle vehicle = db.Vehicles.Find(id);
        if (vehicle == null)
        {
            return HttpNotFound();
        }
        return View(vehicle);
    }

    //
    // POST: /Vehicle/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
        Vehicle vehicle = db.Vehicles.Find(id);
        db.Vehicles.Remove(vehicle);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

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

1 个答案:

答案 0 :(得分:0)

使用延迟加载器你可以做到这非常简单

查看车辆索引中的下拉列表

您的数据类/型号

    public virtual Status Status { get; set; }
    [ForeignKey("Status")]
    public int StatusId { get; set; }

索引控制器

在逻辑类中,您应该有一个返回所有状态列表的方法 和一个返回车辆清单的类

  ViewBag.Status = new SelectList(logic.getALLStatus(), "StatusId","Name");
  return View(Vehiclelogic.GetALLVehicles.tolist());

然后只需添加视图,让Scaffolding完成其余的工作:) 当您在不同的层中编写逻辑代码时,它会使生活变得更加容易,并有助于单元测试:)