0

I can successfully make a jQuery Ajax call into my C# Controller and receive back an XML string, but I need to in turn gather some Portfolio dates and package them up into a JSON object so I can send them back into another C# Controller. If it's a C# issue, then I apologize if I'm in the wrong forum...however I'd like to pass my JSON object into the server side controller ..

Here's what I'm trying to do:

        var nodeDatesJson = {"nodedates":        // CREATE JSON OBJECT OF DATE STRINGS
                            { "date": 01/20/2012,
                              "date": "01/21/2012" } };
        getTradeContribs(thisPfId, nodeDatesJson.nodedates.date);     

Now call the next js function:

 function getTradeContribs(pfid, nodedates) {
        //alert(nodedates);
        $.ajax({                                // GET TRADE CONTRIBS FROM SERVER !!
            url: "/Portfolios/getTradeContribs?portfolioId=" + pfid + "&nodedates=" + nodedates,
            type: "GET",    // or "PUT"
            dataType: "json",               
            async: true,
            success: parseTradeContribs,
            error: function (error) {
                alert("failed in opening Trade Contribs file !!!");
            }
        });
    }
    function parseTradeContribs(data) {
        alert("In parseTradeContribs..." );
        $(data).find("Trade").each(function(){
            $(".TradeContrib").append($(this).text());
        })
    }

and my C# controller is trying to read in the "nodedates" JSON object, but HOW do I read it in ?

   public string getTradeContribs(string portfolioId, **string nodedates**)
        {

            // Build Portfolio Select request here !
            RequestBuilder rzrRequest = new RequestBuilder();

            // REQUEST FOR CONTRIBUTIONS !
            // ... more code here..
            xmlResponse.LoadXml(contribResponse);

            string jsonTest = @" {""nodedates"": ""date"":""01/01/2012""}";

            //return xmlResponse.OuterXml;  // WORKS FINE
            return "<Trade><TradeId>1234</TradeId></Trade>";    // RETURN TEST XML STR

        }

thank you in advance... Bob

bob.mazzo
  • 5,183
  • 23
  • 80
  • 149

2 Answers2

1

The best way to receive a list of dates in a MVC action is to bind to a collection. What this means is that you should put your dates and other attributes in a form with the following naming convention:

<input type="hidden" name="dates" value="2012-1-20" />  
<input type="hidden" name="dates" value="2012-1-21" />  

Then you should serialize this form (look into jquery's docs for this) and post its data to your action, which will be something along the lines of:

public ActionResult getTradeContribs(string portfolioId, IList<DateTime> dates) {
  // Do your work here
}  

You should really take a look into MVC Model binding and collection binding as well:
Model binding to a list
Model binding objects


Also, if I may, your javascript object has two properties with the same name, which is probably not what you mean. If you want to have multiple dates stored somewhere in a object, you should use an array:

    var nodeDatesJson = {"nodedates":  
                        [ "01/20/2012", "01/21/2012" ] };  
Marcelo Zabani
  • 2,139
  • 19
  • 27
0

Sorry, but I didn't understand your doubt very well...but here it goes:

Maybe you should pass the json, well-formatted, as a string and use some C# parser.

This way you can get a object in server-side as same as the Json object in javascript.

=]

Vitor Braga
  • 2,173
  • 1
  • 23
  • 19
  • the server side is exactly the problem. I'm trying to figure out how to read in the json object in my c# code...i.e. public string getTradeContribs(string portfolioId, dynamic nodedates) – bob.mazzo Jul 26 '12 at 21:24
  • Unfortunately I don't know C#...But doesn't C# have some function to parse a string into a Json object?? Or doesn't have a function to parse a string into a C# object?? Like Java has Jackson, Gson libraries that do that... =/ – Vitor Braga Jul 26 '12 at 21:33
  • This [link](http://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object) helps how to convert a string to a c# object... – Vitor Braga Jul 26 '12 at 21:43
  • There's another [link](http://stackoverflow.com/questions/2859753/what-is-simpliest-c-sharp-function-to-parse-json-string-into-object) here... – Vitor Braga Jul 26 '12 at 21:45
  • Look at @MarceloZabani answer below...it seems being correct..Gained my upvote =] – Vitor Braga Jul 27 '12 at 17:29
  • Thanks Victor. His array idea worked out better. I'm now trying to parse out the "nodedates" in C# as follows... – bob.mazzo Jul 27 '12 at 21:57
  • var nodes = ser.Deserialize(nodedates); foreach (var date in nodes) { string theKey = date.Key; string thisNode = date.Value[0]; } – bob.mazzo Jul 27 '12 at 21:58
  • but I'd like to use a clean iDictionary type...however not sure how to implement... – bob.mazzo Jul 27 '12 at 21:58
  • I'll contact Marcelo again...he can help you better than me...hauhuahhua – Vitor Braga Jul 27 '12 at 23:33