mardi 22 décembre 2020

-ASP DOT NET CORE- I want to have access to the list of CheckBoxes (Value) from my View on my Controller's public async Task

In my view I have a code similar to this:

@model C.Models.CM
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div class="form-group">
                <label asp-for="Date" class="control-label"></label>
                <input asp-for="Date" class="form-control" />
                <span asp-validation-for="Date" class="text-danger"></span>
            </div>

            <div class="row">
                <select data-placeholder="Select Item" asp-for="IdItem" class="form-control" asp-items="ViewBag.Items"></select>
            </div>

            <div class="form-group">
                <input id="cboAll" type="checkbox" value="CheckAll / unCheck" /> All<br />
                @foreach (var cdos in ViewBag.Modelon.Cdos)
                { 
                    <input id=@cdos.Id type="checkbox" value=@cdos.Id class="w3-check" />@cdos.Name<br />
                }
            </div>

            <div class="form-group">
                <input id="cboAll" type="checkbox" value="CheckAll / unCheck" /> All<br />
                @foreach (var ctres in ViewBag.Modelon.Ctres)
                {
                    <input id=@ctres.Id type="checkbox" value=@ctres.Id class="w3-check" />@ctres.Name<br />
                }
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

The important thing to understand here is that, this Code generates an amount N of CheckBox which depend on data inserted on the Database, in addition to other attached information in the view, the view looks like this:

My View

So, as you see the amount of CheckBox goes from 1 to N. My problem is the following, I can't understand how to obtain the CheckBox group data in my controller. I don't even know if this information is actually being sent to the controller. I have the following code in my controller:

        public async Task<IActionResult> Create(List<string> slct)
    {
        System.Diagnostics.Debug.WriteLine("test");
        foreach (string value in slct)
        {
            System.Diagnostics.Debug.WriteLine(value);
            };
        System.Diagnostics.Debug.WriteLine("test");

        return RedirectToAction(nameof(Index));
    } 

I would like that my code does certain things depending on the information that comes from the CheckBoxs, right now I was trying to see if all the data from the View was being sent as a slct List, but that isn't the case because this code doesn't print anything in the debug screen except for the "test".

I've done my research, and people recommended the following links to learn how to load related data in the controller.

https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/read-related-data?view=aspnetcore-5.0 https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/update-related-data?view=aspnetcore-5.0

The problem is that no matter how much I read the tutorials, I can't understand them or solve how to apply them to my case, because they work with a much more complex example, and the fact that the code I need to use is of the type await _context.Model.Include(a => a.Thing1).Include(b => b.Thing2) .ThenInclude(c => c.Whatiscwhatisbwat??) certainly doesn't help (the writing of that code is very confusing for me and I am having problems understanding what it does).

So, Ok someone could help me with an explanation somewhat simpler than the one from the tutorials, to solve this? In the method that I mention from the controller, I want to have access to the two groups of CheckBoxs from the View and their selected values, in any format (like a List, Array, etc), I know that I must implement an include code but I don't know how to do it, or if there is a simpler solution.




Aucun commentaire:

Enregistrer un commentaire