複数の多対多の関係を持つエンティティをどのように更新できるのか疑問に思っていました。したがって、多くのコネクタ、入力電圧、レンズタイプ、およびライトカラーを備えた車のモデルがあるとします。ユーザーが 1 つのページですべての選択を更新できるようにしたいので、ページを送信すると、すべてのコネクタ、ライトなどが車に追加または削除されます。ViewModel を使用して ViewModel に各エンティティを配置するか、view-bag を使用してアイテムのリストを渡すかを考えていました。ユーザーが保存すると、他の誰かが車に何かを追加または削除した場合、ページ自体が再表示されます。C# と Entity Framework 6 で MVC5 を使用しています
モデル例
using System;
using System.Collections.Generic;
namespace Bob.Models
{
public partial class Car
{
public Car()
{
this.Connectors = new List<Connector>();
this.InputVoltages = new List<InputVoltage>();
this.LensTypes = new List<LensType>();
this.LightColors = new List<LightColor>();
}
public int Id { get; set; }
public string Name { get; set; }
public byte[] RowVersion { get; set; }
}
public ICollection<Connector> Connectors { get; set; }
public ICollection<InputVoltage> InputVoltages { get; set; }
public ICollection<LightType> LensTypes { get; set; }
public ICollection<LightColor> LightColors { get; set; }
}
}
コントローラ アクションの例
public ActionResult Edit(int id)
{
ViewBag.PossibleLightColors = lightcolorRepository.All;
return View(carRepository.Find(id));
}
[HttpPost]
public ActionResult Edit(Car car)
{
if (ModelState.IsValid) {
carRepository.InsertOrUpdate(car);
carRepository.Save();
return RedirectToAction("Index");
} else {
ViewBag.PossibleLightColors = lightcolorRepository.All;
return View();
}
}