0

It seems things have become more complex going from ASP.NET MVC to .NET Core because I can no longer easily send List of objects to controller using Ajax. Am I doing something wrong?

In my controller, I have this method:

[HttpPost("EditMultipleResults")]
[Consumes("application/x-www-form-urlencoded")]
public bool EditMultipleResults([FromForm] List<Result>, [FromForm] string comment)
{
    // do something...
    return true;
}

Result is defined here

public class Result
{
    [Key]
    public long taskcd { get; set; } 
    public long Runno { get; set; }
    public string Workorder {get; set;}       
}

In my JS Ajax I have:

var results = [
{taskcd: 123,
 Runno: 187776876,
 Workorder: 'VA1234567'
},
{taskcd: 642,
 Runno: 187776877,
 Workorder: 'VA1234569'
},
{taskcd: 766,
 Runno: 187776876,
 Workorder: 'VA1234564'
}
];


 var posteddata = {
            results: results,
            comment: 'test comment'
        };

// call the controller
$.ajax({
            type: 'POST',         
            data: posteddata,            
            traditional: true,
            contentType: 'application/x-www-form-urlencoded; charset=utf-8',          
            url:  'api/ResultsEditor/EditMultipleResults',
            success: function () {
                deferred.resolve();
            },
            error: deferred.reject
        });
        return deferred.promise();

My problem is that results list and reason are null when in controller. How can I pass a list of objects to controller in .NET Core 5?

Another question: is there a way to see the data that's being passed to controller in dev tools?

halfer
  • 19,824
  • 17
  • 99
  • 186
sarsnake
  • 26,667
  • 58
  • 180
  • 286

2 Answers2

1

you can't use [FromForm] (as well as [FromBody]) multiple times, your action input parameter should be the same as it is in ajax, so you will have to create a class

   public class PostedData
    {
        public List<ResultItem> Results { get; set; }
        public string Comment { get; set; }
    };

and action

public bool EditMultipleResults(PostedData data)
{
    // do something...
    return true;
}

and you don't need to use [Consumes("application/x-www-form-urlencoded")] ,[FromForm] and contentType: 'application/x-www-form-urlencoded; charset=utf-8' since all of them are already by default.

Serge
  • 40,935
  • 4
  • 18
  • 45
  • thank you - yes I did that as well and it worked for awhile and now no longer working. This used to be so easy in old .net, I have no idea why I can not make this work. Shouldn't be so difficult – sarsnake Dec 22 '22 at 23:28
0

Because you are creating form data as a json object manually, instead of posting an html form, you need to encode that data as x-www-form-urlencoded manually also.

This post has some great suggestions. Make sure to read not just the accepted answer.

Neil W
  • 7,670
  • 3
  • 28
  • 41
  • this is a lot of reading to simple pass a couple of variables - something that was easy and smooth. MS always makes things worse. – sarsnake Dec 22 '22 at 23:30