Pass the (integer) Return from a Controller method to the View

Asked

Viewed 485 times

3

I have this method in a controller:

int GetSpot()
{
    List<CompanyDetail> topCompanies = GetTopCompanies();
    CompanyDetail topCompany1 = topCompanies.Where(x => x.Company.TopCompany == 1).FirstOrDefault();
   if (topCompany1 == null) return 1;
    CompanyDetail topCompany2 = topCompanies.Where(x => x.Company.TopCompany == 2).FirstOrDefault();
    if (topCompany1 == null) return 2;
    CompanyDetail topCompany3 = topCompanies.Where(x => x.Company.TopCompany == 3).FirstOrDefault();
    if (topCompany1 == null) return 3;

    return -1;
}

I want it now in a view Razor call this method and put the result into a javascript variable. How can I do this?

Update:

I’m now trying to do with json, but I’m not getting it. Look what I have on view:

$.ajax({
            url: '/Home/SpotNR',
            type: 'POST',
            success: function (result) {

                topSpot = result.Data;

            }

        });

and in the controller:

public JsonResult SpotNR()
{
  int spot = -1;
        IList<CompanyDetail> topCompanies = PresentationServices.Helper.GetCompaniesAll();
        CompanyDetail topCompany1 = topCompanies.Where(x => x.Company.TopCompany == 1).FirstOrDefault();
        if (topCompany1 == null) spot = 1;
        CompanyDetail topCompany2 = topCompanies.Where(x => x.Company.TopCompany == 2).FirstOrDefault();
        if (topCompany1 == null) spot = 2;
        CompanyDetail topCompany3 = topCompanies.Where(x => x.Company.TopCompany == 3).FirstOrDefault();
        if (topCompany1 == null) spot = 3;


    JsonResult returnObj = new JsonResult
    {
        Data = new
        {
            Spot = spot
        }
    };

    return Json(returnObj);
}

The problem is I don’t understand why it doesn’t work, but he never goes to success (topSpot remains Undefined) but the answer on Chromedevtools is like this:

{"ContentEncoding":null,"ContentType":null,"Data":{"Spot":3},"JsonRequestBehavior":1,"MaxJsonLength":null,"RecursionLimit":null}

The number of spot is correct, so he processed the method well.

  • In Success do: topSpot = result.Data.Spot;

  • It does not reach the Success, goes to error instead...which may be wrong?

  • Asks for error parameters (are 3, I think) and printa on the screen to see.

  • parse error and no Conversion from text to application/json

  • Puts dataType: 'json' in ajax and forehead

  • Thanks for the help @jbueno ! So back to Success no problem. I’ll see the differences from application/json for json.

  • Great then. If you want, you can see something at this link

Show 2 more comments

1 answer

3


Views do not call from actions, at least not directly. It seems to me that you are trying to summarize a desktop application behavior for a web application.

What you can do is request a server and return a JSON with the value returned from its function.

Example:

No Controller:

public JsonResult GetSpot()
{
    int result = -1; //seu código deve setar o valor de result
    return Json(new { data = result }, JsonRequestBehavior.AllowGet);
}

In the View:

$.ajax({
    url: '@Url.Action("GetSpot", "Controller")',
    data: data,
    type: 'GET',
    dataType: 'json',
    success: function(response){
        var variavel = response.data; //Aqui está o retorno do controller
    },
    error: function(){
        // Algo deu errado
    }
});

Browser other questions tagged

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