Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda