locked
Convert Output to Json RRS feed

  • Question

  • User-2110954851 posted

    Hello everyone!

    I need your help.

    I have this code:

    @BuildTreeView(treeView, IdCategoriaParente)
    
    
    @helper  BuildTreeView(IEnumerable<dynamic> data, int catfiglia, int? idcategoriaparente = 1, int level = 0, string accumulatore = "")
    4.{
    5. 
    6.    //
    7. 
    8.    //
    9.    var selectRecord = "";
    10.    var nodes = data.Where(n => n.IdCategoriaParente == idcategoriaparente).OrderBy(n => n.Id);
    11.    var separatore = "";
    12. 
    13.    if (nodes.Any())
    14.    {
    15.        if (nodes.First().IdCategoriaParente != null)
    16.        {
    17.            if (level > 0)
    18.            {
    19.                separatore = " >> ";
    20.            }
    21.        }
    22. 
    23.        if (level == 0)
    24.        {
    25.            /*
    26.            @Html.DropDownList("Direction", new List<SelectListItem>
    27.{
    28.  new SelectListItem{ Text = "Home To School", Value = "0" },
    29.  new SelectListItem{ Text = "School To Home", Value = "1" }
    30.}, new { onchange = "getAlldata()" })
    31.            */
    32.            @Html.Raw("<select name=\"IdCategoriaParente\" id=\"IdCategoriaParente\">");
    33. 
    34.    @Html.Raw("<option value=\"0\">Nessuna</option>");
    35.        }
    36.        foreach (var node in nodes)
    37.        {
    38.            if (node.Id == catfiglia) { selectRecord = "selected"; }
    39.            //Funzioni.SelezionaSelect(node.Id, catfiglia)
    40.            else { selectRecord = ""; }
    41. 
    42.            var fullText = accumulatore + separatore + node.NomeCategoria;
    43.            @Html.Raw(string.Format("\r\n<option value=\"{0}\" {2}>{1}</option>", node.Id, fullText, selectRecord));
    44. 
    45.    @BuildTreeView(data, catfiglia, node.Id, level + 1, fullText);
    46.        }
    47.        if (level == 0)
    48.        {
    49.            @Html.Raw("\r\n</select>");
    50.        }
    51.    }
    52.    else
    53.    {
    54.        // @Html.Raw("<select name=\"IdCategoriaParente\" id=\"IdCategoriaParente\"><option value=\"0\">Nessuna</option></select>");
    55.    }
    56.}
    

    It is working properly. The problem is that now, the result is printed on the screen with a select.

    I wish the result was made into a JSON format: {"...", "...", "...": ...}

    Can you help me?

    Thanks in advance

    Saturday, June 20, 2015 1:34 PM

All replies

  • User-821857111 posted

    Can you give an example of your expected output?

    By the way, you shouldn't need to use Html.Raw in Razor helpers. They output raw HTML by default.

    Sunday, June 21, 2015 3:48 AM
  • User-2110954851 posted

    Hi Mike!

    The output It should be like this:

    output: "[{"catA" : "NameCatA", "IdCatA" : "ValueIdCatA"}{"catB" : "NameCatB", "IdCatB" : "ValueIdCatB"}]]

    Thanks!

    Sunday, June 21, 2015 7:12 AM