lundi 30 octobre 2023

Html form returns Model with null attribute ins asp.net core

I'm currently developing an html asp.net core web app that requires a huge form. This form is basically a large treeview with selectable items inside which are not hardcoded but retrieved form a .db file.

To clarify things, the "treeView" looks like this: Treeview capture

And the html code that creates it is the following one:

<form method="post" action="/Launch/Home/retrieveSelection">
    <input type="number" name="Id" value="1234" style="display:none" />
    @*MIRAR PERQUE tvList SEMPRE ESTA A NULL*@
    <ul>
        @foreach (TVStandard tvstandard in Model.tvList.StandardsList) {
            int i = 0;
            <li>
                <span class="caret"></span>
                <input type="checkbox" class="checker" id="@tvstandard.Name" name="tvList.StandardsList[@i].IsSelected" value="true">
                <label for="@tvstandard.Name">@tvstandard.Name</label>
                <ul class="nested">
                    @foreach (TVSection tvsection in tvstandard.Children) {
                        int j = 0;
                        <li>
                            <span class="caret"></span>
                            <input type="checkbox" class="checker" id="@tvsection.Name" name="tvList.StandardsList[@i].Children[@j].IsSelected" value="true">
                            <label for="@tvsection.Name">@tvsection.Name</label>
                            <ul class="nested">
                                @foreach (TVMode tvmode in tvsection.Children) {
                                    int k = 0;
                                    <li>
                                        <span class="caret"></span>
                                        <input type="checkbox" class="checker" id="@tvmode.Name" name="tvList.StandardsList[@i].Children[@j].Children[@k].IsSelected" value="true">
                                        <label for="@tvmode.Name">@tvmode.Name</label>
                                        <ul class="nested">
                                            @foreach (TVFreq tvfreq in tvmode.Children) {
                                                int l = 0;
                                                <li>
                                                    <ul class="nested" style="display:block">
                                                        <input type="checkbox" class="checker" id="@tvfreq.Name" name="tvList.StandardsList[@i].Children[@j].Children[@k].Children[@l].IsSelected" value="true">
                                                        <label for="@tvfreq.Name">@tvfreq.Name</label>
                                                    </ul>
                                                </li>
                                                l++;
                                            }
                                        </ul>
                                    </li>
                                    k++;
                                }
                            </ul>
                        </li>
                        j++;
                    }
                </ul>
            </li>
            i++;
        }
    </ul>
    <br />
    <input type="submit" />
</form>

Warning: I've only shown the form itself, the page also has its css and javascript to make the treeview work as I like to.

Also, here it is the Model:

public class MeasureModel
{

    public int Id { get; set; }
    public TVList? tvList { get; set; }
}

Also, it is worth mentioning that this list is made of classes that themselves contain more classes. All of them inherit from:

public class TVItem
{
    public string Name;
    public TVItem Parent;
    public List<TVItem> Children;
    public bool IsSelected = false;

    public TVItem()
    {
        Children = new List<TVItem>();
    }

    public TVItem(string name, TVItem parent)
    {
        Children = new List<TVItem>();
        this.Name = name;
        this.Parent = parent;
    }
    public TVItem(TVItem item)
    {
        this.Children = new();
        Name = item.Name;
        Parent = item.Parent;
        IsSelected = item.IsSelected;
    }
}

Finally, as you may see the model that I use to create this treeview is already initialized with its values and the only thing that I want is to SELECT or UNSELECT them, nothing else. I assumed that the model submitted by the form would be the same than the one used to build the form itself.

So my questions are:

  1. Is the submitted form the same as the page one?
  2. In case 1 is false, do I need to fill every attribute of it in order to make the tvList not NULL?
  3. Which are the ways to do it? (I find using the hidden attribute very ugly but if it is the way I don't ming using it)

Thanks for your help in advance.




Aucun commentaire:

Enregistrer un commentaire