Return JSon value in C #

3

Good morning / afternoon / night SOes,

It's been several days since I've been trying to make a statement and I can not hit the key.

The topic is the following :

I have to add to a Contract depending on which one, a letter in front: "A" or "B", I am using MVC. I have my method in the controller which receives as a parameter a Type, ID, year and month.

[HttpGet]
public JsonResult Cto(string Tipo, int IdProveedor, int Anio, int Mes)
{
switch (Tipo) {

case "A":
return new JsonResult() { Data = servicio.GetContratoA(Id, Anio, Mes).ToList(), JsonRequestBehavior = JsonRequestBehavior.AllowGet };

case "B":
return new JsonResult() { Data = servicio.GetContratoB(Id, Anio, Mes).ToList(), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }
 }

Now, this JSon method is called by my Vista Index with an AJAX function.

function LoadCto(tipo) {

The data brings me perfectly, the question is: How do I add the letter I want (either A or B) ahead of these contracts?

OUTPUT OBTAINED:

CONTRATO 1

DESIRED DEPARTURE (type A check)

A CONTRATO 1

DESIRED DEPARTURE (type B)

B CONTRATO 5

TIP: I can not add it in the GetContract function called service because it causes me an Entity problem since this method is used in several instances.

    
asked by byte96 09.08.2018 в 16:24
source

3 answers

0

Another option could be to return a complex type that has your list and type in it. something like that.

public class TuTipo{
    string Tipo { get; set; }
    List TuLista{ get; set; }
}

[HttpGet]
public JsonResult Cto(string Tipo, int IdProveedor, int Anio, int Mes)
{
    var dataResult = new TuTipo() { Tipo = Tipo };

    switch (Tipo) {
    case "A":
        dataResult.TuLista = servicio.GetContratoA(Id, Anio, Mes).ToList();
    case "B":
        dataResult.TuLista = servicio.GetContratoB(Id, Anio, Mes).ToList();
            }
return new JsonResult() { Data = dataResult, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
     }

On the other hand as a recommendation, you might consider making the type an enum and not having hard code.

I did not try this code but I hope it is understood, in case of any doubt I remain attentive.

    
answered by 13.08.2018 в 14:27
0

I add you as I would, a bit similar to @manu_rit , but with some changes.

public class ContratoCollection
{
    [JsonProperty("tipo")]
    public string Tipo { get; set; }
    [JsonProperty("contrato")]
    public Contrato Contrato { get; set; }

    public ContratoCollection() { }
}

[HttpGet]
public object Cto(string Tipo, int IdProveedor, int Anio, int Mes)
{
    ContratoCollection cc = new ContratoCollection() { Tipo = Tipo};

    switch (Tipo)
    {
        case "A":
            cc.Contrato = servicio.GetContratoA(Id, Anio, Mes).ToList();
            break;
        case "B":
            cc.Contrato = servicio.GetContratoB(Id, Anio, Mes).ToList();
            break;
    }

    var obj = JsonConvert.SerializeObject(cc);

    return obj;
}

To use the JsonConvert.SerializeObject make sure all objects have a constructor empty.

    
answered by 13.08.2018 в 14:48
-1

Try doing the following:

[HttpGet]
public JsonResult Cto(string Tipo, int IdProveedor, int Anio, int Mes)
{
    switch (Tipo) 
    {

        case "A":
            var a = "A";
            return Json(new { letter = a, Data = servicio.GetContratoB(Id, Anio, Mes).ToList(), , JsonRequestBehavior = JsonRequestBehavior.AllowGet });
        case "B":
            var b = "B";
            return Json(new { letter = b, Data = servicio.GetContratoB(Id, Anio, Mes).ToList(), , JsonRequestBehavior = JsonRequestBehavior.AllowGet });
    }
}

you declare a variable within case where you save the letter in this case A or B then send it as json to view .

Another way would be to declare the variable outside the switch and within each case just fill it with the corresponding data.

    
answered by 09.08.2018 в 17:38