Get values in javascript of a session array in c #

0

Hi, I would like to know how I can get the javascript values of a Session variable of c# that contains an array of strings. The code is the following: C# :

if (Session["PolizasClientes_RowID"] != null)
{
  Session.Remove("PolizasClientes_RowID");
}
var array = lstPolizas.Where(x => x.POLIZA == row_id).Select(x => x.DNI).ToArray();
var array2 = lstPolizas.Where(x => x.POLIZA == row_id).Select(x => x.POLIZA).ToArray();
string dni= array[0];
string poliza = array2[0];

string[] arrays = new string[] { dni, poliza };
Session["PolizasClientes_RowID"] = arrays;

Code javascript to read the function that does not work for me (it returns System.string [1] or something like that if I read the entire session and if I put [0] it returns S):

var valores_sesion = '<%=Session["PolizasClientes_RowID"]%>';
//me devuelve System.string[1] o algo asi:
console.log(valores_sesion);
//me devuelve S
console.log(valores_sesion[0]);
//me devuelve S
console.log(valores_sesion[0][0]);

If instead of adding an array to the session, I add a single value if it works correctly, that is, with a code in c# :

Session["PolizasClientes_RowID"] = arrays;

code in javascript :

var valores_sesion = '<%=Session["PolizasClientes_RowID"]%>';                 
console.log(valores_sesion);

The result is the correct DNI.

    
asked by wuasaa 24.03.2017 в 21:26
source

2 answers

0

Try this:

<script>
    var valores_sesion = '<%= Session["PolizasClientes_RowID"].ToString() %>';
</script>

If it does not work, there is a way to iterate the array that you save in the session by doing something like the following:

var values = [];

<% 
    foreach(var item in Session["PolizasClientes_RowID"] { 
%>    
       values.push('<%=item%>');    
<% 
    }
%>
    
answered by 24.03.2017 в 22:14
0

You have two options that come to mind to perform this task of "Render an array in C # in an array of Javascript"

  • OPTION 1: Foreach (idem to what they already propose) Perform a foreach to write or the array in javascript or go agreando with push (also in javascript)
  • OPTION 2: [Recommendation] Serialize the array in JSON (here with JSON.NET you can help us easily)

And also I commented a Tip / Recommendation to work with session (which by the way would have to see why you are using session)

  • Encapsulate Session in Properties (To have intellisense, and typing session)

** Why did not it work directly by typing or Session ToString ()? First of all if you do or have something like that to write session (or any other object that I do not have overwritten ToString ())

<script> 
    var array1 = <%=Session["Array1Demo"] %>;
</script>

Or that is the same as placing ToString ()

<script> 
    var array1 = <%=Session["Array1Demo"].ToString() %>;
</script>

You will have this rendering result      var array1 = System.String[];

Which is not what we are needing, since it does not render to an array but writes the name of the object type that contains the session , which is the default behavior of the ToString ( ) in object, write the full name of the class. But you can write about , this I leave as a concern for you to investigate, since in object-oriented languages you can (when the same class leaves you) define a different behavior for a class that inherits from another in one method.

We go to the rendering options ...

OPTION 1 Perform a foreach to write or the array in javascript or go agreando with push (also in javascript) Here instead of writing an array we are literally writing the contents of each item to add it to an array in javascript with PUSH, or even directly assembling the "string" of the array. If good can be done is a somewhat "manual" method taking into account option 2 which is the serialization.

OPTION 2 Serialize the array in JSON (with Here we can use JsonConvert.SerializeObject(Session["Array1Demo"]); , let's see an example helping us with a property in a Webform

We have a property ...

public string Data { get; set; }

Then

Data = JsonConvert.SerializeObject(Session["Array1Demo"]);

And we render

<script> 
        var array1 = <%= Data %>;
    </script>

And the render will be

<script> 
    var array1 = ["item1","item2","item3"];
</script>

Just what we are needing.

You could also have everything in sight

<script> 
    var array2 = <%=  Newtonsoft.Json.JsonConvert.SerializeObject(Session["Array1Demo"]) %>;
</script>

I hope it will help or guide you

Links that can help you

answered by 18.03.2018 в 12:42