jeudi 17 novembre 2016

Get values to [HttpPost] from checkbox in MVC4 View

I have a View() with "Post" strong type. And i pass a list of categories in a ViewBag.categories, like this:

foreach (var cat in db.Category)
        {
            SelectListItem selectListItem = new SelectListItem()
            {

                Text = cat.TITLE,
                Value = cat.ID.ToString(),
                Selected = true ? Tools.postHaveThisCategory(cat.ID, editPost) : false
            };

            listSelectListItem.Add(selectListItem);
        }

        ViewBag.categories = listSelectListItem;

This is my checkbox in View():

<h4>Categories: </h4>
    @using (Html.BeginForm())
    {
        foreach(var item in (ViewBag.categories as List<SelectListItem>)){
        @Html.CheckBox(item.Value, item.Selected, new { value = @item.Value }) <a>@item.Text</a>
        }


    }

Now, I want to get the values from the checked checkboxes. How can i do that, I readed some tutorials, but can't figure it out how to do it in this case, note that I started working with c# and MVC 1 week ago, so maybe this is too easy, but not for me.




Aucun commentaire:

Enregistrer un commentaire