Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two entities that have a many-to-many relationship

First Class:
C#
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
C#
 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

C#
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

C#
 [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 /*dex*/)
         {
            ModelState.AddModelError("","Unable to save changes try again, and     if the problem persists see your system administrator");
         }
     }
     // method to provide information for the check box array using the StatutFSEvenement view model class
     PopulateAssignedEventData(statutFicheSinistreToUpdate);
     return View(statutFicheSinistreToUpdate);
 }



Whilst creating or updating a StatutFicheSinitre If I check any of the checkboxes that contain the Events nothing happens.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900