lundi 17 mai 2021

Values of List of checkboxes not posted during submit

I manage to display 2 lines of checkboxes, the 2nd one was initially selected fine on the cshtml view. But after I change the selections on the view and submit, the posted data did not reflect the current selections. It still showed the initial state when the page was constructed. Please help thanks.

I have a data model like this:

public class Check
{
    public List<CheckModel> chkList = new List<CheckModel>()
    {
        new CheckModel("1",false),
        new CheckModel("2",true)
    };
}

public class CheckModel
{
    public string name{get;set;}

    public bool choice{get;set;}

    public CheckModel(string nn, bool bb)
    {
        name=nn;
        choice=bb;
    }

}

The View is like so:

@model Check

@using (Html.BeginForm("abcde","Home", FormMethod.Post)) {

<ul>
    @for(int i=0; i < Model.chkList.Count;i++)
    {
        <li>
            input type="checkbox" asp-for=@Model.chkList[i].choice/>@Model.chkList[i].name
        </li>
    }
</ul>


<input type="submit" value="submit"/>

}

The Homecontroller.cs :

public IActionResult abcde(Check c)
{
    return Ok();
}



Aucun commentaire:

Enregistrer un commentaire