Pass Javascript Array of Objects to C# Codebehind

clamchoda

I am having some problems Posting my Javascript Array of objects to C# Codebehind. I followed a simple tutorial and thought this would work quite nicely but my C# codebehind breakpoints in PassThings is never hit.

I have tried changing the url to "Default.aspx/PassThings" but it still never gets posted to my code behind, the error alert displays "[object object"]

Here is my client side:

Default.aspx

Script

<script>

    function Save() {
        $(document).ready(function () {
            var things = [
        { id: 1, color: 'yellow' },
        { id: 2, color: 'blue' },
        { id: 3, color: 'red' }
        ];

                things = JSON.stringify({ 'things': things });

                $.ajax({
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    type: 'POST',
                    url: '/PassThings',
                    data: things,
                    success: function () {
                        alert("success");
                    },
                    error: function (response) {
                        alert(response);
                    }
                });
            });

    }

</script>

Html

<input type="button" value="Pass Things" onclick="JavaScript: Save();">

Default.aspx.cs

Codebehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Services;

[System.Web.Script.Services.ScriptService]
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public void PassThings(List<Thing> things)
    {
        var t = things;
    }

    public class Thing
    {
        public int Id { get; set; }
        public string Color { get; set; }
    }

}

Does anyone see what I am doing wrong?

Thank you for your time.

Ehsan Sajjad

In url pass proper url with page. Suppose the PassThings method is in Default.aspx page code behind file then you have to pass url: Default.aspx/PassThings if the script code is written within the Default.aspx.

If script in seperate js file which is in Scripts folder, then you have to come back one directory and have to write : url: ../Default.aspx/PassThings

$(document).ready(function () {
    var things = [{
        id: 1,
        color: 'yellow'
    }, {
        id: 2,
        color: 'blue'
    }, {
        id: 3,
        color: 'red'
    }];

    things = JSON.stringify({
        'things': things
    });

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: 'Default.aspx/PassThings',
        data: things,
        success: function () {
            alert("success");
        },
        error: function (response) {
            alert(JSON.stringify(response));
        }
    });
});

and in code behind your method should be decorated with [WebMethod] and it should be public and static:

    [WebMethod]
    public static void PassThings(List<Thing> things)
    {
        var t = things;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pass the values from javascript to Codebehind

From Dev

objective c - pass array of objects to table view

From Dev

JavaScript: call C# codebehind function

From Dev

Pass array of objects by reference and save the actual array in JavaScript

From Dev

Pass a parameter from parent to child ASP GridView (C# codebehind)

From Dev

Pass array of objects containing arrays to MVC action via javascript

From Dev

Pass associative array of objects

From Dev

Pass javascript array to C# via json

From Dev

Pass javascript array to C# via json

From Dev

Pass a C# custom array to JavaScript

From Dev

How to display array from C# codebehind to ASPX page?

From Dev

C++: Objects created via array, but how to pass parameters?

From Dev

C++: Objects created via array, but how to pass parameters?

From Dev

Can't get Javascript to fire from c# codebehind

From Dev

Pass Array to Javascript Array C# ASP.NET

From Dev

Pass a dynamic array of objects to function

From Dev

pass an array of objects in sling servlet

From Dev

Pass Array of Objects in http Post

From Dev

Pass array of objects to POST in Angular

From Dev

Pass-by-reference JavaScript objects

From Dev

How to pass the SelectedValue from a DropDownList as a query string in a LinkButton with/without using CodeBehind in ASP.net C#

From Dev

Pass Python array to javascript?

From Dev

Pass a java array to javascript

From Dev

Pass an object into an array in JavaScript

From Dev

Pass Python array to javascript?

From Dev

Javascript Pushing Objects to Array

From Dev

Filling an array with objects in Javascript

From Dev

Transforming a JavaScript array of objects

From Dev

Javascript: read Array of Objects

Related Related

HotTag

Archive