3 Replies - 1249 Views - Last Post: 02 December 2010 - 03:29 AM Rate Topic: -----

#1 W3bDev   User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Json To method

Posted 09 November 2010 - 10:42 AM

Hello all,

I posted this yesterday to the MVC forum, but there has been no reply, and that forum looks like it's pretty stale, so I figured I'd post here.

I'm trying to pass a Javascript array to a JsonResult method, but continually receive 'null' values no matter if I change the type to list or an array. Interesting though, if I change the type to something outside of array or list (single variable), an exception is thrown for that method because it doesn't see any data being passed for that variable.

Firebug result of the post:

UPCs[] 12345678
UPCs[] 82314128
UPCs[] 23492382

I've also tried changing the list/array type from string, to int, but still receive a null result.

Javascript to send the data on click:
            $("#removeUPCs").click(function () {
                var upcArray = new Array();

                upcArray[0] = 12345678;
                upcArray[1] = 82314128;
                upcArray[2] = 23492382;

                $.post("/PO/DeleteUPCs", { UPCs: upcArray },
                function (data) {
                    // success
                }, "json");

            });




[AcceptVerbs(HttpVerbs.Post)]
        public virtual JsonResult DeleteUPCs(Int64[] UPCs) // have tried different types, lists, always null
        {
            string upcList = "";

            foreach(Int64 upc in UPCs){
                upcList += upc + ", ";
            }

            return Json(new { Result = upcList });

        }


This post has been edited by W3bDev: 09 November 2010 - 10:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Json To method

#2 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Json To method

Posted 09 November 2010 - 11:34 AM

maybe try

$.post("/PO/DeleteUPCs", { UPCs[]: [12345678, 82314128, 23492382] },
    function (data) {
        // success
    }, "json");



or possibly
$.post("/PO/DeleteUPCs", { UPCs[]: upcArray },
    function (data) {
        // success
    }, "json");



haven't had a chance to test either though
Was This Post Helpful? 0
  • +
  • -

#3 W3bDev   User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: Json To method

Posted 09 November 2010 - 11:57 AM

I tried those but still wouldn't work :(...

But I did find a way around :gunsmilie:

Just went ahead and passed a comma delimited string, replaced the whitespace, trimmed the extra comma, and split the numbers at the comma's and have the same result as I was shooting for.

UPCs = UPCs.Replace(" ", "");
UPCs = UPCs.TrimEnd(new char[] { ',' });
string[] UPCsToDelete = UPCs.Split(new char[] { ',' });

Was This Post Helpful? 0
  • +
  • -

#4 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Json To method

Posted 02 December 2010 - 03:29 AM

Hi W3bDev,

The solution is as follows and works 100%.

In your javascript code create a variable that is of type Object and NOT OF TYPE Array:

var array = Object();
array[0] = 100;
array[1] = 200;
array[2] = 300;


The in your MVC controller your method should look like this:

public ActionResult Test(IEnumerable<int> data)
{
    return null;
}


Your AJAX call then looks like this:

$.post('/Home/Test', {data: array}, function(data){alert(data);});

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1