samedi 24 octobre 2020

Pass a list of checkboxes to controller in ASP.NET MVC to get a list of related items

In my project, I have generated a list of related filter options to each product category and shown them as a partial view named AdvancedSearch in the Index view:

@model IPagedList<fardashahr.Models.ProductModel>
@using PagedList.Mvc;
@using PagedList;
<link href="~/Content/PagedList.css" rel="stylesheet" />
<div class="w-100">p
</div>
<div class="row w-100">
    <div class="col-md-3 bg-light">
            @Html.Partial("AdvancedSearch")
    </div>
    <div class="col-md-9">
            @foreach (var item in Model)
            {
                <div class="col-md-3">
                        <a href="@Url.Action("Details","Home",new { name = item.ProductUrl })">
                        </a>
                </div>
            }
        <div class="row">
            @Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page = page, name = ViewBag.categoryName,search=ViewBag.Search , pageSize=ViewBag.PageSize }))
        </div>
    </div>
</div>

And in AdvancedSearch partial view, I need to send chechboxes, their value and whther they are checked or not to the controller. Here is the partial view:

    @using fardashahr.Models;
@model FilterVM
@{
    List<SpecItemsModel> options = ViewBag.filters as List<SpecItemsModel>;
}
@if (options != null)
{
    <h3>Advanced Search</h3>
    <form action="Home/Index" method="get">
        @foreach (SpecItemsModel spec in options)
        {
            string label = spec.Name;
            int id = spec.Id;
            string name = spec.LatinName + "_" + id;

            object val = spec.SpecValue != null ? spec.SpecValue : string.Empty;

            <div class="w-100 form-group">
                <h4 class="active">
                    @Html.Label("", label, new { @class = "w-100 p-2" })
                </h4>
                <div class="w-100 p-2">
                    @switch (spec.SpecType.TechCode)
                    {
                        //some other cases
                          
                        case "SelectList":
                            SelectList selectList = SelectLists.GetDynamicList(spec.ListType.TechCode, val);
                            List<CodingItemModel> checkboxes = SelectLists.GetOptions(spec);
                            foreach (CodingItemModel item in checkboxes)
                            {
                                <div class="checkbox">
                                    @Html.CheckBox(item.Name, false, new { @id = id, @name = item.TechCode, @class = "" })
                                    @Html.Label(item.Name)
                                </div>
                            }
                            break;
                        default:
                            break;
                    }
                    @Html.ValidationMessage(name, "", new { @class = "text-danger" })
                </div>
            </div>
        }
        <div class="">
            <input type="submit" class="btn btn-danger btn-block btn-lg" value="Search" />
        </div>
    </form>
}

The action method should be something like this:

public ActionResult Index(string name, int? page, string search = null, int pageSize = 5)
        {
//some code
}

The problem is: I don't know how to send selected checkboxes by user to the controller. I tried FormCollection which only sends values. And since I cannot include isCheched property to CodingItemModel (representing every filter item), I tried a ViewModel including an IsCheched property but in this case such error appears: the Index view needs an IpagedList of ProductModel not a ViewModel. Any Ideas? I appretiate that.




Aucun commentaire:

Enregistrer un commentaire