locked
passing a json object to a new cshtml page RRS feed

  • Question

  • User-250171173 posted

    Hello, 

    I'm hoping for a little guidance or an example of how I might send a json object to a new cshtml page from different page. eg (datalist.cshtml -> itextsharp.cshtml)

    Let me explain the scenario:  I have a cshtml page that uses datatables.js to display a dataset (datalist.cshtml). I can filter the datatable and then create a json file of the filtered results.

    var result = dtable.rows( { filter: 'applied' } ).data().toArray()
    var myJsonString = JSON.stringify(result);

    Now this is where I'm stuck, I want to send this (myJsonString) to a new .cshtml page (itextsharp.cshtml) and open it in the browser, and then have the json file available to use in c#, something like 

    var reader = new StreamReader(Request.InputStream);
    var jsonExt = reader.ReadToEnd();

    I'm using iTextSharp on the new .cshtml page to generate a pdf from the incoming json file.

    I'm really not sure how to approach sending from javascript and receiving on a new .cshtml page.

    Any help would be appreciated. 

    Tuesday, March 3, 2015 4:18 AM

Answers

  • User-250171173 posted

    I found another of Mikes articles which uses a hidden input and form post as a suggested way to get a value to another page.

    This is the javascript code I'm using to generate the form post action (it grabs the datatables filtered items and creates a json string, then send to a new page)

            $('#content').on('click', '#Report', function () {
                console.log('Report - clicked');
                var x = dtable.rows( { filter: 'applied' } ).data().toArray()
                var myJsonString = encodeURIComponent(JSON.stringify(x));            
                           
                 $('<form action="Test?projectno=43" method="post">' + 
                    '<input type="hidden" name="jsonData" id="jsonData" value="' + myJsonString + '">' +
                    '<input type="hidden" name="tdata" id="tdata" value="Test">' +
                  '</form>')
                    .appendTo($(document.body)) //it has to be added somewhere into the <body>
                    .submit();        
            });

    On the new page here is my code to receive the json string.

    @using Newtonsoft;
    @using Actions.Models;
    @using System.Web.Script.Serialization;
    @{  
    
        //var jsonData = Json.Decode(HttpUtility.UrlDecode(Request["jsonData"]));           
        var result = new JavaScriptSerializer().Deserialize<List<tblProjectActionsView>>(HttpUtility.UrlDecode(Request["jsonData"]));     
        //@ObjectInfo.Print(jsonData);
        @ObjectInfo.Print(result);
     
    }

    Here is a link with further details on the C# solution. 

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 4, 2015 12:16 AM

All replies

  • Tuesday, March 3, 2015 4:23 AM
  • User-250171173 posted

    Hello Mike,

    I need to load the POST page to the browser screen, how would I do that?

    My POST page generates a pdf while is why returning the result to the same page wont work.  

    Thanks

    Tuesday, March 3, 2015 5:36 AM
  • User-250171173 posted

    I found another of Mikes articles which uses a hidden input and form post as a suggested way to get a value to another page.

    This is the javascript code I'm using to generate the form post action (it grabs the datatables filtered items and creates a json string, then send to a new page)

            $('#content').on('click', '#Report', function () {
                console.log('Report - clicked');
                var x = dtable.rows( { filter: 'applied' } ).data().toArray()
                var myJsonString = encodeURIComponent(JSON.stringify(x));            
                           
                 $('<form action="Test?projectno=43" method="post">' + 
                    '<input type="hidden" name="jsonData" id="jsonData" value="' + myJsonString + '">' +
                    '<input type="hidden" name="tdata" id="tdata" value="Test">' +
                  '</form>')
                    .appendTo($(document.body)) //it has to be added somewhere into the <body>
                    .submit();        
            });

    On the new page here is my code to receive the json string.

    @using Newtonsoft;
    @using Actions.Models;
    @using System.Web.Script.Serialization;
    @{  
    
        //var jsonData = Json.Decode(HttpUtility.UrlDecode(Request["jsonData"]));           
        var result = new JavaScriptSerializer().Deserialize<List<tblProjectActionsView>>(HttpUtility.UrlDecode(Request["jsonData"]));     
        //@ObjectInfo.Print(jsonData);
        @ObjectInfo.Print(result);
     
    }

    Here is a link with further details on the C# solution. 

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 4, 2015 12:16 AM