jeudi 1 octobre 2015

list of checkbox using mymodel

I try to use a list of checkbox in my view and pass all checked checkbox to my controller like explain here : List of checkbox

but anything is wrong because the list passed to the controller from the view is empty.

I use a personal model:

public class Monmodele
{
    public TTypeRetouche Ttyperetouche { get; set; }
    public List<TTypeRetouche> Ttyperetouches { get; set; }
    public CheckTypeRetouche Cchecktyperetouche { get; set; }
    public List<CheckTypeRetouche> Cchecktyperetouches { get; set; }
    public TRetoucheTypeRetouche Tretouchetyperetouche { get; set; }
    public List<TRetoucheTypeRetouche> Tretouchetyperetouches { get; set; 
}

[NotMapped]
public class CheckTypeRetouche
{
    public int Idtyperetouche {get;set;}
    public string Libelle {get;set;}
    public bool Checked {get;set;}
}

//Table et classe TretoucheTypeRetouche
//Contient tous les types de retouche d'une retouche (une photo)
[Table("TRetoucheTypeRetouche")]
public class TRetoucheTypeRetouche{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public virtual int Idretouche { get; set; }
    public virtual int Idtyperetouche {get;set;}
    public virtual int Idcommande { get; set; }

}

//Table et classe TypeRetouche
[Table("TTypeRetouche")]
public class TTypeRetouche{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Idtyperetouche { get; set; }
    public string Libelle { get; set; }
    public int Prix { get; set; }
    public string SRCtyperetouche { get; set; }
    [NotMapped]
    public HttpPostedFileBase fichierphotomodele { get; set; }
}

In the controller, I retrieve data to display in the view and I add to a list data for the checkbox :

    public ActionResult VCreateCommande()
    {            
        using (var db = new ApplicationDbContext())
        {
            if (Request.IsAuthenticated)
            {
                string userid = User.Identity.GetUserId().ToString();
                monmodel.Tretouchetyperetouches = (from a in db.TRetoucheTypeRetouches
                                                   where a.Idcommande == idcmd
                                                   select a).ToList();
                monmodel.Ttyperetouches = db.TTypeRetouches.ToList();

                //Liste des type de retouche pour les checkbox
                var list = new List<CheckTypeRetouche>();

                foreach (var item in monmodel.Ttyperetouches)
                {
                    list.Add(new CheckTypeRetouche{Idtyperetouche=item.Idtyperetouche,Libelle=item.Libelle,Checked=false});
                }
                monmodel.Cchecktyperetouches = list;

            }
            return View(monmodel);
        }
    }

The POST in the controller, I pass a list of checkbox ojects:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult VCreateRetouche([Bind(Include = "SRCPhoto,fichierphoto,Idtyperetouche")] TRetouche Tretouche, List<CheckTypeRetouche>list)
    //public ActionResult Create()
    {
        using (var db = new ApplicationDbContext())
        {
            if (Request.IsAuthenticated)
            {
        ......

                //Récupérer l'identifiant de la retouche crée
                //Récupération de l'identifiant

                idretouche = db.TRetouches.Max(u => u.Idretouche);

                //Quelles sont le ou les types de retouches sélectionnées

                foreach (var item in list)
                {
                    if (item.Checked == true)
                    {
                        TRetoucheTypeRetouche tretouchetyperetouche = new TRetoucheTypeRetouche();
                        tretouchetyperetouche.Idretouche = idretouche;
                        tretouchetyperetouche.Idcommande = idcmd;
                        tretouchetyperetouche.Idtyperetouche = item.Idtyperetouche;
                        db.TRetoucheTypeRetouches.Add(tretouchetyperetouche);
                    }
                }


            }
            return View("VCreateCommande",monmodel);
        }
    }

And the view :

@using (Html.BeginForm("VCreateRetouche", "CCommande", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="creation-retouche">
        <h4>Specifier votre retouche</h4>
        @foreach (var item in Model.Cchecktyperetouches)
        {
            <div>
                <div class="ilnb col1">
                    @Html.HiddenFor(model => item.Idtyperetouche)
                    @Html.DisplayFor(model => item.Libelle)
                </div>
                <div class="ilnb col2">
                    @Html.CheckBoxFor(model => item.Checked)
                </div>
            </div>
            }
        <div class="">
            <div class="ilnb col1">Photo...</div>
            <div class="ilnb col2">
                @Html.TextBoxFor(model => model.Tretouche.fichierphoto, new { type = "file" })
            </div>
        </div>
        <br />
        <div>
            <input type="submit" value="Ajouter" class="inpajout btn btn-default" />
        </div>
        <br />
    </div>
}

The model used in the view is :

@model CreationLogicielSitePhoto.Models.Monmodele




Aucun commentaire:

Enregistrer un commentaire