dimanche 14 janvier 2018

mvc5 post custom List Object to Controller from View

I am currently attempting to post a list of a custom object to the controller. I am able to successfully display this data from the controller to the View, but am not able to have the data post back.

Any suggestions would be greatly appreciated.

List

public List<Role> Roles { get; set; }

    public class Role
    {
        public string Name;
        public bool isActive;

        public Role() { }

        public Role(string roleName, bool status)
        {
            Name = roleName;
            isActive = status;
        }
    }

View

<!--User Role-->
                    <div class="form-group row">
                        <div class="offset-2 col-md-8">
                            <label class="col-form-label-lg gf-input-title">Roles</label>
                            @if (User.IsInRole("Admin"))
                            {

                            <div class="form-group">
                                <div class="btn-group btn-group-toggle" data-toggle="buttons">
                                    @for (var i = 0; i < Model.Roles.Count; i++)
                                    {
                                        var active = Model.Roles[i].isActive ? "btn btn-primary active" : "btn btn-primary";
                                    <label class="@active">

                                        @Html.HiddenFor(x => x.Roles[i].Name)
                                        @Html.DisplayFor(x => x.Roles[i].Name)
                                        <input type="checkbox" name="@Model.Roles" checked="@Model.Roles[i].isActive" />
                                    </label>
                                    }
                                </div>
                            </div>
                            }
                        </div>
                    </div>

Result in controller After Submit




Aucun commentaire:

Enregistrer un commentaire