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

162
Vistas
Ajax returning null value / asp.net

I am trying to get multiple entiries with Ajax. However, both "id" and "qty" return null (By the way, I gave default values for them now.). Can you please help me? Please review my code below.

<script type="text/javascript">
$(document).ready(function () {
    $("#submit").on("click", function () {
        var elem = {};
        elem.qty = "10";
        elem.id = 1;
   
        $.ajax({
            url: '/Home/Form1/',
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(elem),
            
            success: function (data) {
                alert(data),
                    console.log(data),
        
                    $("#output").html(data[0]);
            }
        });
    });
});

    public class Data
{

    public string qty { get; set; }
    public int? id { get; set; }
}

        public ActionResult Form1(Data elem)
    {

        List<Data> Elements = new List<Data>
        {
            new Data {qty=elem.qty, id=elem.id},
            };


        return Json(Elements);
    }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

"Lets focus on below code first"

        var elem = {};
        elem.qty = "10";
        elem.id = 1

Note: As you can see it already on Json fashion so you do not need to parse into json again. Therefore no need to use JSON.stringify in this case. So you could try below way:

Javascript Code:

<script>
        $(document).ready(function () {
            $("#btnSubmit").on("click", function () {
                var elem = {};
                elem.qty = "10";
                elem.id = 1;

                console.log(elem);
                $.ajax({
                    url: 'https://localhost:44361/userLog/FormPost',
                    type: 'POST',
                    dataType: 'json',
                    data: elem,
                    success: function (data) {
                        alert(data),
                            console.log(data),

                            $("#output").html(data[0]);
                    }
                });
            });

        });
    </script>

C# Controller Code:

        [HttpPost]
        public async Task<IActionResult> FormPost(Data elem)
        {
            List<Data> Elements = new List<Data>
        {
            new Data {qty=elem.qty, id=elem.id},
            };


            return Json(Elements);

        }

Output:

enter image description here

Hope it would resolved your problem as expected.

over 4 years ago · Santiago Trujillo 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