I have two entities that have a many-to-many relationship
First Class:
public class StatutFicheSinistre
{
public int StatutFicheSinistreID { get; set; }
public string codeStatutFS { get; set; }
public DateTime DateSurvenance { get; set; }
[Display(Name = "Date de Declaration")]
public DateTime DateDeclaration { get; set; }
public int VehiculeID { get; set; }
public int FicheSinistreID { get; set; }
public virtual Vehicule Vehicule { get; set; }
public virtual FicheSinistre FicheSinistre { get; set; }
public virtual ICollection<Evenement> Evenements { get; set; }
public virtual ICollection<DocDeclaration> DocDeclarations { get; set; }
}
second class
public class Evenement
{
public int EvenementID { get; set; }
public string CodeEvenement { get; set; }
public virtual ICollection<StatutFicheSinistre> StatutFicheSinistres { get; set; }
}
I have created the following ViewModel StatutFSEvenement
public class StatutFSEvenement
{
public int EvenementID { get; set; }
public string CodeEvenement { get; set; }
public bool designee { get; set; }
}
In the First Class controller,in the edit, index and create actions, I have retrieved the events that are related to a certain statutFicheSinistre. If I manually insert data in the database it's shown correctly, however, if I try to update or create a new StatutFicheSinistre and Associate evnts with it it doesn't work.
I think I should specify in the EvenementController but I don't know how.
this is the POST Edit Action in the Controller
[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public ActionResult EditPost(int? id, string[] selectedEvents)
{
var statutFicheSinistreToUpdate = db.StatutFichesSinistres
.Include(i => i.FicheSinistre)
.Include(i=>i.Evenements)
.Where(i => i.StatutFicheSinistreID == id)
.Single();
if (TryUpdateModel(statutFicheSinistreToUpdate, "",
new string[] { "codeStatutFS", "DateSurvenance", "DateDeclaration",
"Vehicule","FicheSinistre"}))
{
try
{
if(String.IsNullOrWhiteSpace(statutFicheSinistreToUpdate.Vehicule.Immatriculation))
{
statutFicheSinistreToUpdate.Vehicule = null;
}
UpdateSinistreEvenement(selectedEvents, statutFicheSinistreToUpdate);
db.Entry(statutFicheSinistreToUpdate).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}catch(RetryLimitExceededException )
{
ModelState.AddModelError("","Unable to save changes try again, and if the problem persists see your system administrator");
}
}
PopulateAssignedEventData(statutFicheSinistreToUpdate);
return View(statutFicheSinistreToUpdate);
}
Whilst creating or updating a StatutFicheSinitre If I check any of the checkboxes that contain the Events nothing happens.