【问题标题】:MVC 4 Model not populating dropdownsMVC 4 模型不填充下拉列表
【发布时间】:2015-07-24 13:51:59
【问题描述】:

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

每辆车都有一个院子和一个从填充的下拉列表中选择的状态。在为 Vehicle、Yard 和 Status 生成控制器后,我添加了一个 Status 记录和一个 Yard 记录,但它们没有显示在 Vehicle 的下拉列表中。下拉列表仍然是空的,我不确定为什么。我尝试了一个类似的模型,它生成没有问题,但我似乎在这里遗漏了一些东西。

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

【问题讨论】:

  • 显示您的剃须刀代码/您将模型集合绑定到下拉列表的位置。
  • 请问您如何将模型传递给视图。
  • 在哪里可以找到将模型集合绑定到下拉列表的剃须刀代码?我让 VS 为控制器生成代码,所以我不确定它会存储在哪里。
  • 是 VehicleContoller 中的内容吗?
  • 右击动作->去查看

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


【解决方案1】:

使用惰性加载器可以很简单地做到这一点

看看在车辆索引中传递一个下拉列表

您的数据类/模型

    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 完成其余的工作 :) 当您在不同的层中编写逻辑代码时,它会使生活变得更加轻松,并有助于单元测试:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    相关资源
    最近更新 更多