Cshtml .NetCore Passing Boolean Input element of an object list

Viewed 31

I have the code below in the cshtml file. The goal is for the user to edit the Boolean field RestrictDocumentViewForCurrentDocument which is inside of the object UsersonHearing which is a list of UsersModel. I am getting the UsersonHearing field to be populated on post through the hidden fields, but its not mapping the new boolean value to the objects. Im suspecting its part of the name field on the input, but I dont know what its suppose to be or if its possible?

                        @for (int i = 0; i < Model.UsersonHearing.Count; i++)
                        {
                            <tr>
                                <td>
                                    @Model.UsersonHearing.ElementAtOrDefault(i).BuiltFullName
                                </td>
                                <td>
                                    @Model.UsersonHearing.ElementAtOrDefault(i).EmailAddress
                                </td>
                                <td>
                                    @Model.UsersonHearing.ElementAtOrDefault(i).AttorneyRole
                                </td>
                                <td>
                                    
                                    <input name=" @Model.UsersonHearing[i].UserID" asp-for="@Model.UsersonHearing.ElementAtOrDefault(i).RestrictDocumentViewForCurrentDocument" type="checkbox" />                                                              
                                    
                                    @Html.HiddenFor(Model => Model.UsersonHearing[i].UserID)
                                    @Html.HiddenFor(Model => Model.UsersonHearing[i].Hearing_UsersID)
                                    @Html.HiddenFor(Model => Model.UsersonHearing[i].BuiltFullName)
                                    @Html.HiddenFor(Model => Model.UsersonHearing[i].RestrictDocumentViewForCurrentDocument)                                   

                                </td>
                            </tr>
                         }
        public List<UsersModel> UsersonHearing { get; set; } = new List<UsersModel>();



  public class UsersModel
    {
        public string EmailAddress { get; set; }

        public NameDTO Name { get; set; }

        public string UserType { get; set; }

        public Boolean CourtUser { get; set; }

        public int UserID { get; set; }

        public string AttorneyRole { get; set; }

        public int AttorneyRoleID { get; set; }

        public string BuiltFullName { get; set; }

        public Boolean PendingCreation { get; set; }

        public int? Hearing_UsersID { get; set; }

        public int? Case_UsersID { get; set; }

        public Boolean RestrictDocumentViewForCurrentDocument { get; set; }
}

1 Answers

You could check the doc about modelbinding:

For each property of the complex type, model binding looks through the sources for the name pattern

<input name=" @Model.UsersonHearing[i].UserID" asp-for="@Model.UsersonHearing.ElementAtOrDefault(i).RestrictDocumentViewForCurrentDocument" type="checkbox" />

the name attribute of your checkbox doesn't match RestrictDocumentViewForCurrentDocument , Why you set the name with @Model.UsersonHearing[i].UserID? Could you explain your requirement ?

Update:

I tried with the codes:

public class UsersModel
    {
        public string EmailAddress { get; set; }       

        public string UserType { get; set; }

        public Boolean CourtUser { get; set; }

        public int UserID { get; set; }

        public string AttorneyRole { get; set; }

        public int AttorneyRoleID { get; set; }

        public string BuiltFullName { get; set; }

        public Boolean PendingCreation { get; set; }

        public int? Hearing_UsersID { get; set; }

        public int? Case_UsersID { get; set; }

        public Boolean RestrictDocumentViewForCurrentDocument { get; set; }
    }
    public class ViewModel
    {
        public List<UsersModel> UsersonHearing { get; set; } = new List<UsersModel>();
    }

in Controller:

[HttpGet]
        public IActionResult Page()
        {
            var viewmodel = new ViewModel() { UsersonHearing = new List<UsersModel>() 
            { 
                new UsersModel(){},
                new UsersModel(){}
            } };
            return View(viewmodel);
        }

        [HttpPost]
        public IActionResult Page(ViewModel viewmodel)
        {
            return View();
        }

in View:

@model ViewModel

<form asp-action="Page">
    @for (int i = 0; i < Model.UsersonHearing.Count; i++)
    {
        <input  asp-for="@Model.UsersonHearing[i].RestrictDocumentViewForCurrentDocument" type="checkbox" />
        <br />        
    }
    <input type="submit" value="Submit"/>
</form>

Result:

enter image description here

Related