Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameter list to Umbraco API plugin controller action

Tags:

c#

umbraco

How to pass a list of values to an Umbraco API plugin controller action?

I have the following action in my controller:

namespace web.site.Controllers 
{
    [PluginController("MyObject")]
    public class MyObjectApiController : UmbracoAuthorizedJsonController
    {
        public MyObjectApiController()
        { }

        public void Delete(List<int> ids) {
            foreach (var id in ids)
            {
                // ....
            }
        }
    }
}

I cannot find the right "wire format" for the ids parameter. I've tried the following:

  • ids[0]=123&ids[1]=456;
  • ids[]=123&ids[]=456;
  • ids=123&ids=456.

Every time the action is called but ids is null. What is the right way to serialize a list of values for the action parameter?

like image 926
ps_ttf Avatar asked Aug 23 '26 08:08

ps_ttf


1 Answers

You need to add [FromUri] before controller action parameter, like...

Delete([FromUri]List<int> ids)

to let model binder know to create the list of ids from parameters in the Uri.

All the formats you tried will now work when you send your request

  • ids[0]=123&ids[1]=456
  • ids[]=123&ids[]=456
  • ids=123&ids=456
like image 140
Nkosi Avatar answered Aug 25 '26 22:08

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!