Click here to Skip to main content
15,868,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am making an ajax call on button click ,in this i am passing the values of selected check box values in an entity along with other values,i am not able to fetch the selected check box values in that call.
I am sharing the code
JavaScript
 $.post("@Url.Action("Create", "Role")",  RoleDetails(), function (data) {
            alert("success");
        });

function RoleDetails() {
    var role = {
        "RoleName": $.trim($('#Name').val()),
        "RoleDescription": $.trim($('#Description').val()),
        "RightsList": RightsSelected()
    };
    return role;
}
function RightsSelected() {
    var selectedRights = [];
    $('input[name="cbRight"]:checked').each(function () {
        selectedRights.push(this.value);
    });
    return selectedRights;
}


code

C#
public class Role
    {
        public string RoleName { get; set; }
        public string RoleDescription { get; set; }
        public int[] RightsList { get; set; }
    }
controller :

[HttpPost]
        public ActionResult Create(Role role)
        {
         return View();
        }
    }


Please let me know what are the changes has to be made to make this work.
Thanks in advance
Posted
Updated 18-Sep-14 21:29pm
v2
Comments
Jameel VM 19-Sep-14 4:39am    
i have tested your code in my machine.it will hit correctly in to create action.Please check in browser console if any javascript error
ravithejag 19-Sep-14 5:31am    
i know its correctly hitting the action,but unable to fetch the selected checkbox values from the model(Role)
Jameel VM 19-Sep-14 6:38am    
can you please post the view code also.
Shemeemsha (ഷെമീംഷ) 19-Sep-14 7:23am    
I think it is a selector problem..Post view code

1 solution

try this-

in jquery:-

JavaScript
$(function () {
            $.post("@Url.Action("Create", "Role")", RoleDetails(), function (data) {
                alert("success");
            });

            function RoleDetails() {
                var role = {
                    "RoleName": $.trim($('#Name').val()),
                    "RoleDescription": $.trim($('#Description').val()),
                    "RightsList": JSON.stringify(RightsSelected())
                };
                return role;
            }
            function RightsSelected() {
                var selectedRights = [];
                $('input[name="cbRight"]:checked').each(function () {
                    selectedRights.push(this.value);
                });
                return selectedRights;
            }
        });


and in c#
C#
public class Role
        {
            public string RoleName { get; set; }
            public string RoleDescription { get; set; }
            public string[] RightsList { get; set; }
        }


        [HttpPost]
        public ActionResult Create(Role role)
        {
            return View();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900