#asp.net-mvc #model-view-controller #many-to-many
Вопрос:
Я создал базу данных из Microsoft Management Studio и генерирую классы в коде с помощью ADO, это моя схема базы данных, у меня проблема с отношением m-m. Я хотел бы получить список с несколькими выборками в таблице Samochody
, столбец: Wyposazenie
, но я получаю исключение null. Отношение 1-n работает хорошо: Отношение 1-n Мой класс Wyposazenie.cs:
[Table("Wyposazenie")]
public partial class Wyposazenie
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Wyposazenie()
{
Samochody = new HashSet<Samochody>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id_wyposazenia { get; set; }
[Required]
[StringLength(50)]
public string Nazwa_wyposazenia { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Samochody> Samochody { get; set; }
}
Samochody.cs:
[Table("Samochody")]
public partial class Samochody
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Samochody()
{
Zdjecia = new HashSet<Zdjecia>();
Wyposazenie1 = new HashSet<Wyposazenie>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id_samochodu { get; set; }
[Required]
[StringLength(50)]
public string Tytul { get; set; }
[Required]
[StringLength(50)]
public string Podtytul { get; set; }
public int? Rok { get; set; }
public int? Przebieg { get; set; }
public int? Pojemnosc { get; set; }
[StringLength(200)]
public string Rodzaj_paliwa { get; set; }
[StringLength(200)]
public string Kategoria { get; set; }
[Required]
[StringLength(200)]
public string Marka { get; set; }
[Required]
[StringLength(200)]
public string Model { get; set; }
[StringLength(200)]
public string Wersja { get; set; }
[StringLength(200)]
public string Generacja { get; set; }
[StringLength(200)]
public string Moc { get; set; }
[StringLength(200)]
public string Skrzynia_biegow { get; set; }
[StringLength(200)]
public string Naped { get; set; }
[StringLength(200)]
public string Vin { get; set; }
[StringLength(200)]
public string Emisja_co2 { get; set; }
[StringLength(200)]
public string Typ { get; set; }
public int? Liczba_drzwi { get; set; }
public int? Liczba_miejsc { get; set; }
[StringLength(200)]
public string Kolor { get; set; }
[Column(TypeName = "date")]
public DateTime? Pierwsza_rejestracja { get; set; }
[StringLength(200)]
public string Numer_rejestracyjny { get; set; }
[StringLength(200)]
public string Zarejestrowany_w_polsce { get; set; }
[StringLength(200)]
public string Stan { get; set; }
[StringLength(200)]
public string Kraj_pochodzenia { get; set; }
[StringLength(200)]
public string Opis { get; set; }
[StringLength(200)]
public string Wyposazenie { get; set; }
public int? Kwota { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Zdjecia> Zdjecia { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Wyposazenie> Wyposazenie1 { get; set; }
}
SamochodyController.cs:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id_samochodu,Tytul,Podtytul,Rok,Przebieg,Pojemnosc,Rodzaj_paliwa,Kategoria,Marka,Model,Wersja,Generacja,Moc,Skrzynia_biegow,Naped,Vin,Emisja_co2,Typ,Liczba_drzwi,Liczba_miejsc,Kolor,Pierwsza_rejestracja,Numer_rejestracyjny,Zarejestrowany_w_polsce,Stan,Kraj_pochodzenia,Opis,Wyposazenie,Kwota")] Samochody samochody)
{
if (ModelState.IsValid)
{
db.Samochody.Add(samochody);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(samochody);
}
My Samochody Create View:
<div class="form-group">
@Html.LabelFor(model => model.Wyposazenie, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.Wyposazenie, new SelectList(Model.Wyposazenie1, "Nazwa_wyposazenia"), "Choose", new { @class = "form - control" })
@Html.ValidationMessageFor(model => model.Wyposazenie, "", new { @class = "text-danger" })
</div>
</div>