ASP.NET MVC - Javascript array always passed to controller as null

Posted by Xuan Vu on Stack Overflow See other posts from Stack Overflow or by Xuan Vu
Published on 2010-04-22T01:18:28Z Indexed on 2010/04/22 1:23 UTC
Read the original article Hit count: 505

Filed under:
|
|
|

I'm having some problem with passing a javascript array to the controller. I have several checkboxes on my View, when a checkbox is checked, its ID will be saved to an array and then I need to use that array in the controller. Here are the code:

VIEW:

var selectedSearchUsers = new Array(); $(document).ready(function () { $("#userSearch").click(function () { selectedSearchUsers.length = 0; ShowLoading(); $.ajax({ type: "POST", url: '/manage/searchusers', dataType: "json", data: $("#userSearchForm").serialize(), success: function (result) { UserSearchSuccess(result); }, cache: false, complete: function () { HideLoading(); } }); }); $(".userSearchOption").live("change", function () { var box = $(this); var id = box.attr("dataId"); var checked = box.attr("checked"); if (checked) { selectedSearchUsers.push(id); } else { selectedSearchUsers.splice(selectedSearchUsers.indexOf(id), 1); } }); $("#Send").click(function () { var postUserIDs = { values: selectedSearchUsers }; ShowLoading(); $.post("/Manage/ComposeMessage", postUserIDs, function (data) { }, "json"); }); });

When the "Send" button is clicked, I want to pass the selectedSearchUsers to the "ComposeMessage" action. Here is the Action code:

public JsonResult ComposeMessage(List values) { //int count = selectedSearchUsers.Length; string count = values.Count.ToString(); return Json(count); }

However, the List values is always null. Any idea why?

Thank you very much.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about jQuery