❔ Getting error 500 on form post (using fetch)
I'm trying to do a form post (posting a model), but my controller is not being called. When I check the fetch result on the console, I get a 500 error, but I haven't been able to pinpoint where the problem lies.
The model:
The js object I'm posting/ the fetch post:
The ImagesPath is an array of strings, where I get the values from inputs of the type 'file'.
The controller
Maybe I'm missing some basic concept or basic knowledge? I've been trying to fix this for hours but I haven't been able to
17 Replies
Is
postsToSend
an array of objects that match PostModel
?As far as I can see, yes, they are. I'm submitting an array of objects, in which each object is composed like this:
Try posting JSON instead
Add a header to your fetch
The best I can think of is that somehow the images path are not being treated like a string
What do I expect in the controller's parameter? a string?
No?
ASP will parse it just fine
In fact, JSON is the preferred format
Oh, ok
Didn't know about that
I'll give it a shot, one sec
No success
I keep getting error 500
I tried changing the Model to accept an List of IFormFile instead of string, but it didn't change a thing
I mean, you're not sending files after all
What's your code after changing to JSON?
Open the devtools, the network panel, and see how the request looks
The model is
And I'm expecting a list of the model, so I think it's correct?
Yeah, everything seems fine
Uh, try adding
[FromBody]
attribute to the parameter of your action method...?
[FromBody] List<PostModel> posts
I discovered what the problem was when I was going to put the FromBody attribute, lol
So I had three controllers with the same name, the only difference being that the type of the parameter is different for each of them
I thought it was fine since I wasn't getting any error for ambiguity, but I guess it's not
Ah, that'd do it lol
Btw, adding the
[FromBody]
attribute still triggers the controller
But it receives null value
Thank you for helping me out on this lol
That tip of the json will be of good use, I had no clue that the asp would do all the work and I had to just pass a jsonlmao
Now I receive the object, but it always come empty
yikes
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.