0

I have 2 models log_ligne_voyage and log_ligne_demande_voyage. I'm using CheckBoxs and I want that when I click on the button Planifier that the value of the selected row of the table log_ligne_demande_voyage is added to the table log_ligne_voyage.

This is the image

log_ligne_demande_voyage code :

public partial class log_ligne_demande_voyage
{
    public log_ligne_demande_voyage()
    {
        this.log_ligne_voyage = new HashSet<log_ligne_voyage>();
    }

    public int ID_LIG { get; set; }
    public Nullable<int> ID_TYPE_LIG { get; set; }
    public Nullable<int> ID_ETAT_LIG { get; set; }
    public Nullable<int> NUM_DEM { get; set; }
    public Nullable<int> ID_PER { get; set; }
    public string CMT_LIG { get; set; }
    public Nullable<double> QTE_LIG { get; set; }
    public Nullable<System.DateTime> CrtDateAuto { get; set; }
    public string Lieu_Dem { get; set; }
    public int Num_Dem { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime Date { get; set; }
    [DisplayFormat(DataFormatString = "{0:hh\\:mm}", ApplyFormatInEditMode = true)]
    public TimeSpan Heure { get; set; }
    public Nullable<int> ID_file { get; set; }
}

log_ligne_voyage Model code :

public partial class log_ligne_voyage
{
    public int ID_LIG_voy { get; set; }
    public Nullable<int> ID_TYPE_LIG_voy { get; set; }
    public Nullable<int> ID_ETAT_LIG_voy { get; set; }
    public Nullable<int> NUM_Voy { get; set; }
    public Nullable<int> ID_PER { get; set; }
    public string CMT_LIG_voy { get; set; }
    public Nullable<double> QTE_LIG_voy { get; set; }
    public string Lieu_LIG_voy { get; set; }
    public Nullable<int> ID_File { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime Date_LIG_voy { get; set; }
    [DisplayFormat(DataFormatString = "{0:hh\\:mm}", ApplyFormatInEditMode = true)]
    public TimeSpan Heure_LIG_voy { get; set; }
    public Nullable<System.DateTime> CrtDateAuto { get; set; }
    public Nullable<int> ID_Ligne { get; set; }

    public virtual log_ligne_demande_voyage log_ligne_demande_voyage { get; set; }
    public virtual log_voyage log_voyage { get; set; }
    public virtual log_type_demande log_type_demande { get; set; }
    public virtual log_personnel log_personnel { get; set; }
    public virtual log_voyage_file log_voyage_file { get; set; }
    public virtual log_voy_etat log_voy_etat { get; set; }
}

Controller code :

    [HttpPost] 
    public ActionResult LigneCheked(/*IEnumerable<log_ligne_demande_voyage> LigneVoyage*/ int[] selectedligne, log_ligne_demande_voyage  item)
    {
        try
        {
            if (Session["login"] == null)
            {
                return RedirectToAction("Index", new { controller = "Login", action = "Index" });
            }

            login = Session["login"].ToString();
            log_voyage model = new log_voyage();
            model.UserCrt_Voy = login;
            model.Dat_Voy = DateTime.Today;
            entities.Set<log_voyage>().Add(model);
            entities.SaveChanges();
            //fine the bigest ID of the voyage
            int MaxID = int.MaxValue;
            foreach (DataRow db in log_voyage.Rows)
            {
                int ID_Voy = db.Field<int>("Num_Voy");
                MaxID = Math.Max(MaxID, ID_Voy);
            }
            int intIdt = entities.log_voyage.Max(u => u.Num_Voy);

            foreach (var item1 in selectedligne)

            {
                 log_ligne_voyage model1 = new log_ligne_voyage();
                    item.ID_ETAT_LIG = 3;
                    model1.Date_LIG_voy = item.Date;

                    model1.CMT_LIG_voy = item.CMT_LIG;
                    model1.ID_Ligne = item.ID_LIG;
                    model1.ID_TYPE_LIG_voy = item.ID_TYPE_LIG;
                    model1.ID_PER = item.ID_PER;
                    model1.QTE_LIG_voy = item.QTE_LIG;
                    model1.Lieu_LIG_voy = item.Lieu_Dem;
                    model1.ID_File = item.ID_file;
                    model1.Heure_LIG_voy = item.Heure;
                    model1.ID_ETAT_LIG_voy = item.ID_ETAT_LIG;
                    model1.NUM_Voy = intIdt;
                    entities.Set<log_ligne_voyage>().Add(model1);
                    entities.SaveChanges();
                    return RedirectToAction("Index", "CreationVoyage");

                }
        }
        catch (DataException /* dex */)
        {
            ModelState.AddModelError("", "Impossible d'executer cette action. contacter le service info SVP.");

            return View("CreationVoyage");
        }
        return View("CreationVoyage");
    }
}

View Code :

 @foreach(var item in Model)
            {
        <tr>
            <td>

                <input type="checkbox" id="checkAll"  name="selectedligne" value="@item.ID_LIG" />

            </td>

1 Answer 1

1

This is the way i think you can do it :

In the view, give all your checkboxes with the same name and a unique value.

<input type="checkbox" name="SelectedLignes" value="@item.ID_LIG" />

In the controller action method, pass an List with the name of the checkboxes.

[HttpPost]
public ActionResult LigneCheked(IList<int> SelectedLignes)
{
   // Receive only those item that were selected
   var selectedItems = Db.log_ligne_demande_voyage.Where(s => SelectedLignes.Contains(s.ID_LIG));
   foreach (var item in selectedItems)
   {
     // Now map item with log_ligne_voyage and add with Db context
     var newItemToSave = new log_ligne_voyage{
        //-----------
     };
     Db.log_ligne_voyage.Add(newItemToSave);
   }
   Db.SaveChanges();
   return RedirectToAction("Display"); //Or something like it.
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , Merci beaucoup pour votre aide :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.