如何在asp.net mvc中设置一对多的关系?

时间:2016-11-02 07:50:48

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-5

我正在尝试在我的asp.net mvc app中进行此设置:

  1. 我正在使用AspNetUser来处理用户。
  2. 一位用户可以拥有多辆汽车
  3. 一辆车可以进行多次维修
  4. 车型

    public class Car
    {
        [Key]
        public int CarID { get; set; }
    
        [Required]
        public virtual ApplicationUser ApplicationUser { get; set; }
    
        public virtual ICollection<Maintenance> Maintenances { get; set; }
    
        [Required]
        public string Brand { get; set; }
    
        // a bunch of other string properties related to a car...
    }
    

    维护模式

    public class Maintenance
    {
        public int CarID { get; set; }
    
        [Key]
        public int MaintenanceID { get; set; }
    
        public int Mileage { get; set; }
    
        public DateTime EntryDate { get; set; }
    
        public DateTime ExitDate { get; set; }
    
        public decimal Cost { get; set; }
    
        public virtual Car Automobile { get; }
    }
    

    IdentityModels.cs

    public class ApplicationUser : IdentityUser
    {
        public virtual ICollection<Car> Cars { get; set; }
    
        // ...
    }
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }
    
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<Car>()
                .HasRequired(c => c.ApplicationUser)
                .WithMany(t => t.Cars)
                .Map(m => m.MapKey("OwnerID"));
        }
    
        public DbSet<Car> Cars { get; set; }
        public DbSet<Maintenance> Maintenances { get; set; }
    }
    

    我查看了表定义,它们似乎没问题(OwnerID外键已正确设置),但由于某些原因,当我尝试添加新车时这不起作用:

    public ActionResult Create(Car car)
    {
        if (ModelState.IsValid)
        {
            db.Cars.Add(car);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    
        return View(car);
    }
    
    1. ModelState.IsValid始终为false
    2. car.ApplicationUser始终为null
    3. 我是asp.net mvc的新手,有人可以告诉我这里我做错了吗?

1 个答案:

答案 0 :(得分:0)

如果Car.ApplicationUser始终为null,那么我建议您再次查看自己的视图,看看您是否有该属性的字段,我的猜测是你没有。

因为您将Model指定为Controller Action的参数:

public ActionResult Create(Car car)

然后MVC将尝试执行模型绑定,在此过程中,表单字段使用字段名称绑定到Car模型以匹配模型属性。

提交给Controller操作的唯一内容是表单中的内容。

一个选项是将ApplicationUser字段作为隐藏字段包含在表单中,并在Controller Action中将您发送到Create View,您将创建一个新的Car Model并在那里填充ApplicationUser,然后将其传递到View中

@StephenMuecke的另一个选择是创建一个专门用于View的模型,并在View中使用ViewModel:

public class CarVM
{
    public ICollection<Maintenance> Maintenances { get; set; }

    [Required]
    public string Brand { get; set; }

    // a bunch of other string properties related to a car...
}

现在注意表单不需要担心ApplicationUser字段以使ModelState有效,当提交表单时它会将字段绑定到ViewModel中的属性,然后在Post Action您将创建一个Car模型并从发布的ViewModel中的数据填充它,并抓住当前用户填充ApplicationUser,然后从那里开始。