mardi 3 novembre 2015

Retrieve selected checkboxes in an array into the controller

How can i retrieve the selected checkbox in the controller.

This is the main view where the user can choose a request access.

@using (Html.BeginForm("addBatch_CARF", "CARF", FormMethod.Post, new { @name = "register" }))
        {
                @Html.ValidationSummary(true)
            <div id="formAlert" class="alert alert-danger">  
                <a class="close">×</a>  
                <strong>Warning!</strong> Make sure all fields are filled and try again.
            </div>

            var catName = "";
            var displayCan = "";
            var candidates = "";

            for (int i = 0; i < Model.Count; i++)
            {
                if (catName != Model[i].request_category)
                {
                <li class="list-group-item list-group-item-success">
                    @Html.DisplayFor(modelItem => Model[i].request_category)
                    <span class="pull-right" style="margin-right:60px;">Special Instructions</span>
                </li>
                    catName = Model[i].request_category;
                    displayCan = catName;

                }
                if (displayCan == Model[i].request_category)
                {
                    candidates = Model[i].request_name;
                <div class="checkbox_request">
                        @Html.CheckBoxFor(model => model[i].isSelected, new { @class = "is_selected" }) 

                        @Html.DisplayFor(model => model[i].request_name)
                        @if(Model[i].request_name == "Folder Access")
                        {
                            <span class="label label-danger">Pls specify all the drive path. Note: For accessing of drives outside PETC please proceed to Online CARF</span>
                        }
                    <span class="pull-right">
                        @Html.EditorFor(model => model[i].special_instruction)
                    </span>
                    @Html.HiddenFor(model => model[i].request_type_id)
                    @Html.HiddenFor(model => model[i].system_roles_id)
                </div>
                }

            }
            <li class="list-group-item list-group-item-success">
            Access to:
            </li>

        <div id="employeeAdd">
            @{Html.RenderAction("AddRequestor"); }
        </div>

        <p class="request_btn">
            <button type="submit" class="btn btn-primary" id="addbtn">Save</button>
        </p>

}

I have only rendered this view AddRequestor in selecting or adding an employee.

<table class="table table-hover">
    @for (int i = 0; i < Model.Count; i++){
            <tr>
                <th>
                    @Html.CheckBox("checkbox", new { @class = "is_selected" }) 
                    @Html.DisplayFor(model => model[i].FullName)
                    @Html.HiddenFor(model => model[i].Emp_Badge_No)
                </th>
            </tr>    

    }   
</table>

The main goal of this is all the chosen employees must have also all the chosen request access.

        [HttpPost]
        public ActionResult addBatch_CARF(List<Request_Type> list, List<Employees_All_vw> emp, string[] checkboxes)
        {
            foreach (var x in emp)
            {
                int num = 1;
                bool z = Convert.ToBoolean(num);
                if (x.checkbox == z)
                {
                    //add data into CARF table
                    CARF carf = new CARF();
                    carf.requestor = x.Emp_Badge_No;
                    carf.carf_type = "BATCH CARF";
                    carf.created_by = @User.Identity.Name.Remove(0, 9).ToLower();
                    carf.created_date = System.DateTime.Now;
                    carf.active_flag = true;
                    db.CARves.Add(carf);
                    db.SaveChanges();
                    int id = carf.carf_id;

                    //add data into Request Access Table
                    foreach (var i in list)
                    {
                        int val = 1;
                        bool y = Convert.ToBoolean(val);
                        if (i.isSelected == y)
                        {
                            Request_Access ra = new Request_Access();
                            ra.request_access_id = 1;
                            ra.carf_id = id;
                            ra.request_type_id = i.request_type_id;
                            ra.special_instruction = i.special_instruction;
                            ra.ra_assignee = i.system_roles_id;
                            ra.dept_approval = null;
                            ra.dept_approval_date = null;
                            ra.dept_remarks = null;
                            ra.final_approval = null;
                            ra.final_approval_date = null;
                            ra.final_remarks = null;
                            ra.acknowledge_by = null;
                            ra.acknowledge_date = null;
                            ra.journal = null;
                            ra.closed_by = null;
                            ra.closed_date = null;
                            ra.verified_by = null;
                            ra.verified_date = null;
                            db.Request_Access.Add(ra);

                        }
                    }
                    db.SaveChanges();
                }

                TempData["MessageAlert"] = "Successfully created!";
                return RedirectToAction("Batch_CARF");
            }
        }

I've got an error on this line if (x.checkbox == z)

Operator '==' cannot be applied to operands of type 'string[]' and 'bool'




Aucun commentaire:

Enregistrer un commentaire