1

I have the code below and when I click on submit the list contains all possible models. However, I only want to know the models which have an checkbox selected. How do I do this?

The issue is that Index containts the whole List, but I only want to have the models or selected items from the ones that are selected in the checkbox. In the scenario below I'm also using some hiddenfields to fill the model. Any advice how I can pass the selections to the controller?

HomeController

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using WebApp.Models;

namespace WebApp.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            var viewModel = new MainViewModel()
            {
                SomeInfo = "test",
                SomeModel = new SomeModel
                {
                    Name = "Model1",
                    SomeType1 = new List<SomeType1>
                    {
                        new SomeType1 { Id = "1", Name = "Spinach", Value = "TXT_FLD_SPINA", ExtraInfo = "something1" },
                        new SomeType1 { Id = "2", Name = "Broccoli", Value = "TXT_FLD_BRO", ExtraInfo = "something else5" },
                        new SomeType1 { Id = "3", Name = "Wheatgrass", Value = "TXT_FLD_WHE", ExtraInfo = "something else4" },
                    },
                    SomeOtherType2 = new List<SomeType1>
                    {
                        new SomeType1 { Id = "1", Name = "Apple", Value = "TXT_FLD_APPLE", ExtraInfo = "something" },
                        new SomeType1 { Id = "2", Name = "Banana", Value = "TXT_FLD_BANA", ExtraInfo = "something else" },
                        new SomeType1 { Id = "3", Name = "Tomatoes", Value = "TXT_FLD_TOM", ExtraInfo = "something else2" },
                    }
                }
            };

            return View(viewModel);
        }

        [HttpPost]
        public IActionResult Index(string search, List<SomeType1> SomeType1, string[] SomeOtherType2)
        {
            return View();
        }
    }
}

MainViewModel

using System.Collections.Generic;

namespace WebApp.Models
{
    public class MainViewModel
    {
        public SomeModel SomeModel { get; set; }
        public string SomeInfo { get; set; }
    }

    public class SomeModel
    {
        public List<SomeType1> SomeType1 { get; set; }
        public List<SomeType1> SomeOtherType2 { get; set; }
        public string Name { get; set; }
    }

    public class SomeType1
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Value { get; set; }
        public string ExtraInfo { get; set; }
    }

}

Index.cshtml

@model MainViewModel
@{
    ViewData["Title"] = "Home Page";
}

@using (Html.BeginForm("Index", "Home"))
{
    <div class="text-center">
        <input type="text" name="search" />
        <input type="submit" value="Submit" />
    </div>

    <br />

    for (int i = 0; i < Model.SomeModel.SomeType1.Count; i++)
    {
        <b>Some Type 1</b>
        <div class="checkbox">
            <label>
                <input type="checkbox"
                       name="SomeType1[@i].Value"
                       value="@Model.SomeModel.SomeType1[i].Value" /> @Model.SomeModel.SomeType1[i].Name
                </label>
            </div>
        }

        for (int i = 0; i < Model.SomeModel.SomeOtherType2.Count; i++)
        {
            <b>SomeOtherType2</b>
            <div class="checkbox">
                <label>
                    <input type="checkbox"
                           name="SomeOtherType2"
                           value="@Model.SomeModel.SomeOtherType2[i].Value" /> @Model.SomeModel.SomeOtherType2[i]..Name
                    </label>
                </div>
            }
        }
Jay
  • 189
  • 4
  • 12
  • What I used to do, and this may not be the best or even acceptable way, was hook into the form submit in JS, and in that function gather up all the selected items and pass an array of ids back to the server using a hidden field. You would then change the signature of the POST action with int[] instead of List. – hijinxbassist Sep 30 '21 at 00:43
  • You need to bind your checkbox to a boolean in your view model. Then when submitting, you can either check for the bool values on ghe server and filter it there. Or use an ajax request to build your view model list in JS to send to the server for a post. – Selthien Sep 30 '21 at 02:09
  • Does this answer your question? [How to pass Models to Controller for selected checkbox using MVC?](https://stackoverflow.com/questions/69367121/how-to-pass-models-to-controller-for-selected-checkbox-using-mvc) – Mark Schultheiss Sep 30 '21 at 12:20
  • also https://stackoverflow.com/q/10649454/125981 or https://stackoverflow.com/q/14730746/125981 or some additional info https://stackoverflow.com/q/2860940/125981 – Mark Schultheiss Sep 30 '21 at 12:24

0 Answers0