vendredi 1 avril 2016

When i send my form, my checkboxes values results always NULL

i have a form where i populate some checkboxes from the table HairTags ,

all works fine, i show my checkboxes in my form, but when i choose some of them and i try to send, all the value results NULL and i do not save them ids inside my filed Creationtag

in my model

//CHECKBOXES
public class HairTagModel
{
    //[Key]
    public int Value { get; set; }
    public string Text { get; set; }
    public bool IsChecked { get; set; }
}



public class CreationHairTagsModel
{
    //public Creation Creation { get; set; }
    //public List<HairTagModel> CreationHairTags { get; set; }

    public CreationHairTagsModel()
    {
        Creation = new Creation();
    }
    public Creation Creation { get; set; }

    private ApplicationDbContext creationdb = new ApplicationDbContext();

    public List<HairTagModel> CreationHairTags
    {
        get
        { 
            var HairTagList = creationdb.HairTags.ToList();

            List<HairTagModel> obj = new List<HairTagModel>();

            foreach (var tags in HairTagList)
            {
                obj.Add(new HairTagModel
                {
                    Text = tags.HairTagTitle,
                    Value = tags.HairTagId,
                    IsChecked = false
                });
            }

            return obj;
        }
    }
}

in my page

<div class="col-md-12">
            @for (int i = 0; i < Model.CreationHairTags.Count; i++)
            {
                @Html.CheckBoxFor(m => Model.CreationHairTags[i].IsChecked)
                @Model.CreationHairTags[i].Text
                @Html.HiddenFor(m => Model.CreationHairTags[i].Value)
                @Html.HiddenFor(m => Model.CreationHairTags[i].Text)<br />
            }
        </div>

in my controller

[HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult CreationUpload(CreationHairTagsModel creationbind, IEnumerable<HttpPostedFileBase> files)
        {
            if (ModelState.IsValid)
            {   
            ...CODE HIDDEN...

                               //Tags
                                StringBuilder sb = new StringBuilder();
                                foreach (var item in creationbind.CreationHairTags)
                                {
                                    if (item.IsChecked)
                                    {                                        
                                        sb.Append(item.Value + ",");
                                    }
                                }
                                creationbind.Creation.Creationtag = sb.ToString();

What is wrong in my code please help and explain ?




Aucun commentaire:

Enregistrer un commentaire