How to send a json to a view with ASP .NET MVC

0

I have a driver that sends me information to download a file, currently sent json that way

public JsonResult GetXML(DownloadModel model)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    Dictionary<string, byte[]> d = new Dictionary<string, byte[]>();
    if (getXML.GetXMLComprobante(datosXML, out respuestaXML))
    {
        byte[] archivoPDF = Convert.FromBase64String(respuestaXML.XMLB64);
        d.Add("pdf", archivoPDF);
        return Json(serializer.Serialize(d));
    }
}

and my problem comes when I want to add the name of the file

d.Add("nombre", "documento-de-prueba.pfd");

and in this case I would have to declare another dictionary to send it and I do not like this way of having to add more dictionaries to send multiple data.

Is there any other way to send Json ?

    
asked by JuankGlezz 20.08.2016 в 02:01
source

1 answer

1

Let's see, I hope this example gives you new ideas: D

Model

public class Usuario
{
    public string Nombre { get; set; }
    public string Apellido { get; set; }   
}

Driver

public JsonResult LlamarJson()
    {
        var output = ObtenerListaUsuarios();
        return Json(output, JsonRequestBehavior.AllowGet);
    }

    private List<Usuario> ObtenerListaUsuarios()
    {
        List<Usuario> lUsuarios = new List<Usuario>(){
            new Usuario(){ Nombre = "Juan",  Apellido = "Glezz" },
            new Usuario(){ Nombre = "Pedro", Apellido = "Avila" },
            new Usuario(){ Nombre = "Pedro Miguel", Apellido = "Pimienta Morales" },
            new Usuario(){ Nombre = "Juan", Apellido = "M" },
            new Usuario(){ Nombre = "Luiggi", Apellido = "Mendoza" },
            new Usuario(){ Nombre = "Paul", Apellido="Vargas"},
            new Usuario(){ Nombre = "Alvaro", Apellido = "Montoro" }
        };
        return lUsuarios;
    }

Vista

<script>
   function LlamarJsonDelServidor(){
    $.ajax({
        type: "POST",
        traditional: true,
        async: false,
        cache: false,
        url: '@Url.Action("LlamarJson","Home")',
        context: document.body,
        data: { },
        success: function (result) {                
            $("#resultado").html('');
            for (var i = 0; i < result.length; i++) {
                $("#resultado").append("<li> "+result[i].Nombre+ " " + result[i].Apellido +" </li>");
            }
        },
        error: function (xhr) {
            //debugger;
            console.log(xhr.responseText);
            alert("Error has occurred..");
        }
    });
}
</script>
<input type="button" onclick="LlamarJsonDelServidor();" value="Llamar Json del Server"/>
<div id="resultado"></div>

The call generates in JSON format:

[{Nombre: "Juan", Apellido: "Glezz"}, {Nombre: "Pedro", Apellido: "Avila"},…]
0 : {Nombre: "Juan", Apellido: "Glezz"}
1 : {Nombre: "Pedro", Apellido: "Avila"}
2 : {Nombre: "Pedro Miguel", Apellido: "Pimienta Morales"}
3 : {Nombre: "Juan", Apellido: "M"}
4 : {Nombre: "Luiggi", Apellido: "Mendoza"}
5 : {Nombre: "Paul", Apellido: "Vargas"}
6 : {Nombre: "Alvaro", Apellido: "Montoro"}
    
answered by 20.08.2016 / 02:04
source