dsi
dsi

Reputation: 3359

How to create list of object in jquery?

How to create list of object in jquery ? Example:

Object name: rowdataObject having properties.

NOw, i want to create list of rowdataObject and pass to MVC.

Please suggest me how to create list of object in javascript and pass as argument in controller.

Thanks

Upvotes: 1

Views: 7828

Answers (1)

SpYk3HH
SpYk3HH

Reputation: 22570

<html>
    <head>
        <script type="text/javascript">
            $(function() {  //  this is jQuery's version of `document.onload = function` which basically means, "start doing this when page is loaded"
                //  an Object in jQuery is extremely simple in nature
                var obj = {};   //  this is officially an object
                //  add to the object by simply creating properties
                obj.prop1 = "This  property is a string";
                //  add inner objects just as easy
                obj.class1 = { prop1: "This inner object now has it's own property" }

                //  to pass this to a "controller" in jQuery, you will use a form of $.ajax
                $.ajax({    //  here you start creating ajax options
                    type: 'POST',   //  makes this retrievable via POST on server side, exp: $_POST['keyName']
                    data: JSON.stringify({ keyName: obj }), //  easiest way to send data as key|value
                    url: 'http://www.example.com'   //  here you need the url to your controller
                })  //  now, use jQuery chainability to see results returned from controller
                    .done(function( data, textStatus, jqXHR ) {
                        //  this function will fire only if the controller makes a successful return
                        /*  do work, exp:   */
                        console.log(data); //   this would show the results of what the controller returned in your browser's developer console
                    })
            })
        </script>
    </head>
    <body>

References

Upvotes: 3

Related Questions