简体   繁体   English

如何从单独的视图发布一对多关系?

[英]How to post a one-to-many relationship from separate views?

I have a record class and a car class. 我有唱片班和汽车班。 A record can have multiple cars. 一条记录可以有多辆汽车。 I am trying to use the RecordID as the key between the two. 我正在尝试使用RecordID作为两者之间的键。 On the first view, my form is filling the model for the record class. 在第一个视图上,我的表单正在填充记录类的模型。 I then want to go to a different view and add cars to the record. 然后,我想转到另一个视图并将汽车添加到记录中。 Once I navigate to the second view, how can I tell the cars which record ID to use? 导航到第二个视图后,如何告诉要使用哪个记录ID的汽车? I'm reading about things like ViewModels and Unit of Work. 我正在阅读有关ViewModels和工作单元的信息。 Not sure differences and/or if those are what I'm looking for. 不确定差异和/或这些是否是我要寻找的。 I'll include some code from my models and repository methods. 我将在模型和存储库方法中包括一些代码。 Thanks! 谢谢!

Record.cs Record.cs

namespace Train.Models {
public class Record {
    public int RecordId { get; set; }
    public int Quantity { get; set; }
    public DateTime DateCreated { get; set; }
    public bool IsActive { get; set; }
    public string UserId { get; set; }
    public virtual ICollection<Cars> Cars { get; set; }
}

} }

Cars.cs Cars.cs

namespace Train.Models {
public class Cars {
    public int Id { get; set; }
    public string EmptyOrLoaded { get; set; }
    public string CarType { get; set; }
    //Hopper, flatbed, tank, gondola, etc.
    public string ShippedBy { get; set; }
    //UP(Union Pacific) or BNSF
    public string RailcarNumber { get; set; }
    //public virtual ApplicationUser ApplicationUser { get; set; }
    public string UserId { get; set; }     
    public virtual Record Record { get; set; }

}

} }

RecordRepository.cs RecordRepository.cs

    public void SaveRecord(Record recordToSave) {

        if (recordToSave.RecordId == 0) {
            recordToSave.DateCreated = DateTime.Now;
            _db.Record.Add(recordToSave);
            _db.SaveChanges();

        } else {
            var original = this._db.Record.Find(recordToSave.RecordId);
            original.Quantity = recordToSave.Quantity;
            original.IsActive = true;

            _db.SaveChanges();
        }
    }

CarsRepository.cs CarsRepository.cs

   public void SaveCar(Cars carToSave) {
        if (carToSave.Id == 0) {
            _db.Cars.Add(carToSave);
            _db.SaveChanges();

        } else {
            var original = this.Find(carToSave.Id);
            original.EmptyOrLoaded = carToSave.EmptyOrLoaded;
            original.CarType = carToSave.CarType;
            original.ShippedBy = carToSave.ShippedBy;
            original.RailcarNumber = carToSave.RailcarNumber;
            _db.SaveChanges();
        }

    }

You have to set a foreign key to your car model for the Record ID. 您必须为您的汽车型号设置外键以获取记录ID。 just add another column in the car model for the RecordID. 只需在汽车模型中为RecordID添加另一列即可。

After inserting the Record, you have to return the inserted ID and set this Record ID to the car objects before saving it to the database. 插入记录后,您必须返回插入的ID并将此Record ID设置为汽车对象,然后再将其保存到数据库中。

public void AddCars(int recordID, List<Cars> carsToSave) {
     foreach(Cars car in carsToSave){
        car.RecordID = recordID;
        _db.Cars.Add(car);
     }
     _db.SaveChanges();
}

public void EditCars(List<Cars> carsToEdit){
    foreach(Cars car in carsToEdit){
        Cars editCar = this.Find(car.Id);
        editCar.EmptyOrLoaded = car.EmptyOrLoaded;
        editCar.CarType = car.CarType;  
        editCar.ShippedBy = car.ShippedBy; 
        editCar.RailcarNumber = car.RailcarNumber;  
        editCar.ApplicationUser = car.ApplicationUser;  
        editCar.UserId = car.UserId;  
        _db.SaveChanges();
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM