Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
List always returns null value when sent from controller to index

I've now tried like 20 different ways and Googled the issue but I'm still stuck. My list is always returning a null value. When I debug I can see the values are coming from the database, and everything seems fine.

But, when I use console.log to debug in the view, it is always null.

Here is my model:

public class GarageModel
{
    public int GarageId { get; set; }
    public int Values { get; set; }
}

C# Method:

public List <GarageModel> getRequestType(int garageId)
{
    List<GarageModel> newList = new List<GarageModel>();

    string sql = "SELECT GarageID, RequestProperty FROM GarageCrossRequestType 
                  WHERE GarageID = " + garageId;

    var cmd = new SqlCommand(sql, Connection);

    var dt = new DataTable();
    dt.Load(cmd.ExecuteReader());

    foreach (DataRow i in dt.Rows)
    {
        var model = new GarageModel();
        model.GarageId = Convert.ToInt32(i["GarageID"].ToString());
        model.Values = Convert.ToInt32(i["RequestProperty"].ToString());
        newList.Add(model);
    }

    return newList;
    // returns the values from database 
}

Javascript / jQuery:

 function editGarage(e) {
            e.preventDefault();
            var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
            var garageId = dataItem.GarageId;
            countryId = dataItem.CountryId;
            name = dataItem.Name;
            customerNumberOfEditingGarage = dataItem.CustomerNumber;
            var Values = dataItem.Values;

            $.ajax({
                   type: 'POST',
                   url: '@Url.Action("getRequestType", "Garage")?garageId=' + garageId,
                   dataType: 'JSON',
                   data: {
                       Values: Values, garageId: garageId
                   },
            });
            console.log(garageId, Values);
            // always return correct garageId but null in Values
            // or whatever value is set to Values in the model

What am I missing?

Like I've said I've tried different methods, I've changed types between get/post, I've changed the method to bool, int et cetera, but I'm lost and honestly, not especially good at JavaScript/jQuery either, so I'll appreciate any help I can get.

To specify, the result I would like is simply to see the Values I get from the method/database to be shown when I use console.log to debug in the browser.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your getRequestType method needs only one param and values not a parameter so send only garageId and you need to fill values when your ajax success

$.ajax({
    type: 'POST',
    url: '@Url.Action("getRequestType", "Garage")',
    dataType: 'JSON',
    data: {
        garageId: garageId
    },
    success: function(data){
        values = data;
        console.log(garageId, data);
    }
});

And controller method needs to return JsonResult instead of list

public JsonResult /*List<GarageModel>*/ getRequestType ...


return Json(newList, JsonRequestBehavior.AllowGet);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda