locked
problems with ajax MVC RRS feed

  • Question

  • User-1062853889 posted

    Hello, I working with ajax for send parameters. example:

    addClient.chtml

    var data = {
                    ClientId: 41255,
                    FilaRac: "1",
                    NombreFila: "Carlos"
                };

                $.ajax({
                    url: '/House/Create/',
                    type: 'POST',
                    dataType: 'json',
                    data: data,
                        success: function (data) {
                           
                        },
                        error: function (err) {
                            alert("Error: " + err.responseText);
                        }
                    });

    Model

    public class Clients{

    [Key]
            public int ClientId { get; set; }
            public int FilaRac { get; set; }
            public string NombreFila { get; set; }

    }

    Controller

     [HttpPost]
            public JsonResult Create(Clients clients)
            {
              
             //   foreach (var client in clients) {
                    _context.Clients.Add(clients);
                   _context.SaveChanges();
                 //   }
          

                return Json(clients);
            }

    this is working fine, the problem is when tray send more data, I mean arrays example:

    var data = [{
                    ClientId: 41255,
                    FilaRac: "1",
                    NombreFila: "Carlos"
                },

    {
                    ClientId: 50000,
                    FilaRac: "3",
                    NombreFila: "Gloria"
                },

    {
                    ClientId: 60000,
                    FilaRac: "5",
                    NombreFila: "Ale"
                }];

    data: data, or data: JSON.stringify(data),
     from controller is null

    public JsonResult Create(Clients clients) is null
    public JsonResult Create(Clients[] clients) is null
    public JsonResult Create(List<Clients> clients) is null

    How can fix the problem null data.
    Thanks!!

    Friday, May 14, 2021 3:14 PM

Answers

  • User475983607 posted

    Below is a .NET 5 example working example.

        public class Clients
        {
            public int ClientId { get; set; }
            public int FilaRac { get; set; }
            public string NombreFila { get; set; }
    
        }
        public class AjaxController : Controller
        {
            [HttpGet]
            public IActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public IActionResult Create([FromBody]Clients[] clients)
            {
                return Json(clients);
            }
        }
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    <div>
        <input id="Button1" type="button" value="button" />
    </div>
    
    @section scripts {
        <script>
            $('#Button1').click(function () {
                var data = [{
                    ClientId: 41255,
                    FilaRac: "1",
                    NombreFila: "Carlos"
                },
    
                {
                    ClientId: 50000,
                    FilaRac: "3",
                    NombreFila: "Gloria"
                },
    
                {
                    ClientId: 60000,
                    FilaRac: "5",
                    NombreFila: "Ale"
                }];
    
                $.ajax({
                    url: '/Ajax/Create/',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function (data) {
                        console.log(data)
                    },
                    error: function (err) {
                        console("Error: " + err.responseText);
                    }
                });
    
            });       
        </script>
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2021 3:23 PM

All replies

  • User475983607 posted

    Below is a .NET 5 example working example.

        public class Clients
        {
            public int ClientId { get; set; }
            public int FilaRac { get; set; }
            public string NombreFila { get; set; }
    
        }
        public class AjaxController : Controller
        {
            [HttpGet]
            public IActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public IActionResult Create([FromBody]Clients[] clients)
            {
                return Json(clients);
            }
        }
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    <div>
        <input id="Button1" type="button" value="button" />
    </div>
    
    @section scripts {
        <script>
            $('#Button1').click(function () {
                var data = [{
                    ClientId: 41255,
                    FilaRac: "1",
                    NombreFila: "Carlos"
                },
    
                {
                    ClientId: 50000,
                    FilaRac: "3",
                    NombreFila: "Gloria"
                },
    
                {
                    ClientId: 60000,
                    FilaRac: "5",
                    NombreFila: "Ale"
                }];
    
                $.ajax({
                    url: '/Ajax/Create/',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function (data) {
                        console.log(data)
                    },
                    error: function (err) {
                        console("Error: " + err.responseText);
                    }
                });
    
            });       
        </script>
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2021 3:23 PM
  • User475983607 posted

    ASP.NET Example.  I assume you forgot the content type property in the AJAX function.

        public class Clients
        {
            public int ClientId { get; set; }
            public int FilaRac { get; set; }
            public string NombreFila { get; set; }
    
        }
        public class AjaxController : Controller
        {
            [HttpGet]
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public JsonResult Create(Clients[] clients)
            {
                return Json(clients);
            }
        }
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    <div>
        <input id="Button1" type="button" value="button" />
    </div>
    
    @section scripts {
        <script>
            $('#Button1').click(function () {
                var data = [{
                    ClientId: 41255,
                    FilaRac: "1",
                    NombreFila: "Carlos"
                },
    
                {
                    ClientId: 50000,
                    FilaRac: "3",
                    NombreFila: "Gloria"
                },
    
                {
                    ClientId: 60000,
                    FilaRac: "5",
                    NombreFila: "Ale"
                }];
    
                $.ajax({
                    url: '/Ajax/Create/',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function (data) {
                        console.log(data)
                    },
                    error: function (err) {
                        console("Error: " + err.responseText);
                    }
                });
    
            });
        </script>
    }
    

    Friday, May 14, 2021 3:28 PM
  • User-1062853889 posted

    Hello, thanks your responses.

    is not working example this line

    public JsonResult Create(Clients[] clients)

    send this error  ajax.Model.Clients{0}

    and the [FromBody] is not working, I working with asp.net Core 2.1
    Thanks!!

    Friday, May 14, 2021 3:59 PM
  • User-1062853889 posted

    Thanks your help.

    Now is working, the problem is with [FromBody].

    Thanks for your help.

    Friday, May 14, 2021 6:49 PM