[英]ASP.NET MVC CORE - Many to many relations - Create Views
因此,我遇到了兩個實體之間存在多對多關系的情況。 在中間,有一些字段可以描述關系。 我將通過以下示例簡化情況(粗體表示兩個實體之間的簡單字段:
public class Hobby
{
public int HobbyID {get;set;}
public string Name {get;set;}
public string Type {get;set;}
public ICollection<PersonHobby> PersonHobbies {get;set;}
}
public class Person
{
public int PersonID {get;set;}
public int Age {get;set;}
public string Name {get;set;}
public ICollection<PersonHobby> PersonHobbies {get;set;}
}
public class PersonHobby
{
public int PersonHobbyID {get;set;}
public int PersonID {get;set;}
public int HobbyID {get;set;}
**public int Rating {get;set;}**
}
我的目標是什么? 有一個創建表單,它可以讓我創建一個人並立即將評級歸因於一個或多個愛好。 創建表單應顯示所有可用愛好的輸入字段,但在提交時,應僅存儲包含數據的字段。
我需要幫助來了解實現這一目標的最佳方法。 到目前為止,我嘗試了 Person Create View 和 Controller 以顯示表單中的所有愛好。 我這樣做的方法是在創建 controller 時加載所有愛好並將它們初始化為空。 但是在提交時,我收到了如下錯誤:“模型綁定的復雜類型不能是抽象類型或值類型,並且必須有一個無參數的構造函數。”
謝謝你。
開始的一點建議:我會在您的視圖中使用視圖模型而不是實體類,因為它會為您提供更大的靈活性並幫助您實現目標,而不會開始使用不屬於那里的代碼弄臟您的視圖。 顯然,你可以用你的實體類來做,盡管提供一些改變,但我不推薦它。
我相信你得到的錯誤是,因為你試圖綁定到定義ICollection屬性的實體類,並且 model 綁定器無法確定應該用於實例化它們的具體類型。 例如,為了快速修復,您可以使用具體類型(例如Collection或List )更改您的ICollection 。 但同樣,這是我的第一個建議......
所以,要以正確的方式做到這一點,這就是我要做的:
public class PersonViewModel
{
public int Age {get;set;}
public string Name {get;set;}
public List<HobbyViewModel> Hobbies {get;set;}
}
public class HobbyViewModel
{
public int HobbyID {get;set;}
public string Name {get;set;}
public string Type {get;set;}
public int? Rating {get;set;}
}
public class PersonController
: Controller
{
...
[HttpGet]
public IActionResult Create()
{
PersonViewModel viewModel = new PersonViewModel();
viewModel.Hobbies = this.DbContext.Hobbies.Select(h => new HobbyViewModel() { Name = h.Name, Type = h.Type }).ToList();
return this.View(viewModel);
}
[HttpPost]
public IActionResult Create(PersonViewModel model)
{
if(!this.ModelState.IsValid)
return this.BadRequest(model);
Person person = new Person(){ Age = model.Age, Name = model.Name };
foreach(HobbyViewModel hobbyViewModel in model.Hobbies)
{
//Check if the user has rated the hobby and if not, skip it
if(!hobbyViewModel.Rating.HasValue)
continue;
person.PersonHobbies.Add(new PersonHobby(){ HobbyId = hobbyViewModel.HobbyId, Rating = hobbyViewModel.Rating.Value });
}
this.DbContext.Persons.Add(person);
this.DbContext.SaveChanges();
return this.RedirectToAction(nameof(Index));
}
}
換句話說,在您的個人表單中,為視圖 model 中提供的每個愛好顯示一個表單,並帶有評級輸入。 當用戶發布表單時,檢查所有提供的愛好以檢查用戶是否已對其進行評分(即:檢查是否設置了可為空的 Rating 屬性),如果已設置,則將新的 PersonHobby 添加到生成的 Person 實體。 瞧!
最后,關於您的實體模型的一些建議:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.