Click here to Skip to main content
15,945,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm trying to pass an array of objects from my javascript in my view to my controller.
After some issues I managed to get it to work, at least until the point that my method in the controller get called and my list<> there get correctly filled.

but at the same time my page is not showing and I get HTTP Error 415.

I have read that I need to use [FromFrom] instead of [fromBody]
but then the result of my canvasFields is empty (list Count = 0)

But with [FromBody] when I set a breakpoint in my controller canvasFields is correctly filled only that my page is not showing with Http Error 415

I really have no idea what to do

What I have tried:

JavaScript
$('#btnClick').click(function (e) {
            $.ajax({
                url: "@Url.Action("SaveTemplate")",
                type: 'post',
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(canvasFields),
                success: function () {
                    alert("yes");
                },
                error: function (errMsg) {
                    alert(errMsg);
                }
            })
        });


C#
[HttpPost]
       
        public ActionResult SaveTemplate([FromBody] List<CanvasField> canvasFields)
        {
            return Json(new
            {
                resut = "OK"
            });
        }
Posted
Updated 19-Apr-20 22:55pm
Comments
MadMyche 19-Apr-20 12:45pm    
So what is different from this "solved" problem?
https://www.codeproject.com/Questions/5265193/Asp-net-core-passing-an-array-of-objects-to-contro
Nizar Belhiba 19-Apr-20 15:16pm    
Yep, you are right. BUT to be honest, as always had a breakpoint I did never noticed that the website didn't show up. I always just check if my List in my Controller get propper filled as I wished without continuing above that point.
And meanwhile, I had some other issue to deal with so when I came back and removed the breakpoint I've just noticed the issue
MadMyche 19-Apr-20 15:34pm    
So, is the List in your controller is populated?
Nizar Belhiba 19-Apr-20 15:37pm    
Yes. And every thing is fine. Just the view dont show. And i get the message this function cant be load Http Error 415

1 solution

OK I found the solution
The problem was that the correct route to my view was missing by adding it I solved the problem:
C#
[Route("Template/SaveTemplate")]
       public IActionResult SaveTemplate()
       {
           return View();
       }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900