How to show the Controller output in View c#

Asked

Viewed 64 times

0

I would like how I can show my result that is in the controller method in the 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},
    });
});

IMG OF EXPLANATION: inserir a descrição da imagem aqui

After I choose a campaign date and year and click on search... He will make a post call through ajax to bring the results and show each one in its place as this green !

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

1 answer

0

Some points that can be observed:

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

2) The values you pass from controller to view vc must set in JS. If you’re passing on Model, you can put @Model.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.