简体   繁体   中英

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. 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? I'm reading about things like ViewModels and Unit of Work. 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

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

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

    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

   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. just add another column in the car model for the 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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