How to show Controller result in View c #

0

I would like to show my result that is in the controller method in View.

EX: Controller:

public class SMSTarifado
{
    public int QtdTarifados { get; set; }
    public int QtdEnviados { get; set; }
    public int QtdRespondidos { get; set; }
}

[HttpGet]
    public ActionResult Index()
    {


         ViewBag.tarifados  = "1"
         ViewBag.enviados   = "1"
         ViewBag.respondidos= "1"


        return View();
    }




    [HttpPost]
    public ActionResult Index(string campanhaSelecionada2, string mesAno)
    {
        var campanhaSelecionada = campanhaSelecionada2;
        var mesAnoSelecionado = mesAno.Split('/');



        var result = banco.Database.SqlQuery<SMSTarifado>("SP_CARREGA_GRAFICO_SMS_TARIFADOS @param1, @param2, @param3 ", new SqlParameter("@param1", Convert.ToInt32(campanhaSelecionada)), new SqlParameter("@param2", Convert.ToInt32(mesAnoSelecionado[0])), new SqlParameter("@param3", Convert.ToInt32(mesAnoSelecionado[1]))).ToList();


        foreach (var item in result)
        {
            ViewBag.tarifados   = Convert.ToString(item.QtdTarifados);
            ViewBag.enviados    = Convert.ToString(item.QtdEnviados);
            ViewBag.respondidos = Convert.ToString(item.QtdRespondidos);
        }


        ListaCampanhas();
        return View();        

    }

JQUERY:

var campanhaSelecionada2;
campanhaSelecionada2 = $("#campanhaSMSTarifados option:selected").val();
$("#campanhaSMSTarifados").change(function () {
    campanhaSelecionada2 = $("#campanhaSMSTarifados option:selected").val();
});

var mesAno;
mesAno = $("#mesAno option:selected").val();
$("#mesAno").change(function () {
    mesAno = $("#mesAno option:selected").text();
});



$("#btnBuscar").click(function () {
    $.ajax({
        type: "POST",
        url: "/SMSTarifados/Index",
        data: { campanhaSelecionada2: campanhaSelecionada2,mesAno:mesAno},
    });
});

EXPLANATORY IMAGE:

After I choose a campaign the date and year and click on search ... It will make a post call through ajax so that it brings the results and shown each one in its place like the green one!

But currently nothing appears in the place where the result has to be shown.

    
asked by anonymous 23.07.2018 / 20:57

1 answer

0

Some points that can be observed:

1) Avoid using the ViewBag. Create an object and pass your Model to the View.

2) The values you pass from the controller to the view vc should be set in JS. If you are passing in the Model, you can put @DataVariable Model

3) Try to create a unique responsibility for each method. Do not do much of a method. There are standards that it is highly advised to follow to make the code easy to maintain in the future.

4) Put the Name and Id in the identical form and make sure that the JS is calling the correct id.

    
24.07.2018 / 05:25