AdrianN-4768 avatar image
0 Votes"
AdrianN-4768 asked AdrianN-4768 edited

Pass IEnumerable<Object> to Controller Method

Hi All

I have a page that has a model of IEnumerable<Object>.
I can display the data fine, but would like the function to download that list into a CSV.

I have the method that generates a CSV and downloads it to the device, however I'm having issues with passing the IEnumerable into the Method.

I'm trying to use @Url.Action to try and pass the parameter through, but am having issues with it.

My Method is

public FileContentResult ExportToCSV(IEnumerable<Object> objectList)
// Export to CSV Code...

I've tried finding other sources to help but can't seem to get it working, is Url.Action even the method I should be using?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered AdrianN-4768 edited

Hi @AdrianN-4768,

To pass a List of objects to the controller, you could use the following methods:

  1. Use core Model Binding and Submit button to submit the form.

    For example: submit list of questions to the controller: QuestionIndex.cshtml (you could change the action name, object name or request url to yours)

          @model List<WebApplication6.Models.Question>
             <form asp-action="QuestionIndex" id="mainform" method="POST">
                 @for (var i = 0; i < Model.Count(); i++)
                     <input asp-for="@Model[i].QuestionId" type="hidden" />
                     <input asp-for="@Model[i].QuestionTitle" type="hidden" />
                     <input asp-for="@Model[i].QuestionAnswer" />
                     <br />
                 <input type="submit" class="btn btn-primary" value="Submit">   
             public IActionResult QuestionIndex(IEnumerable<Question> questions)
                 //do something
                 return View(_context.Questions.ToList());

  2. Using JQuery Ajax submits the form data or an array of JavaScript objects to the controller.

    For example: add a button in the above index page, then, in the button click event, use Ajax method to submit the form to the controller.


    Comment: Here I use a screenshot to show the code, because I tried to post the sample code, but it disappears when preview.

The screenshot like this:


More detail information, you could refer my reply in this thread.

If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
If the answer is helpful, please click "Accept Answer" and vote it up.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best Regards,

image.png (24.1 KiB)
2.gif (440.4 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks so much for the reply @ZhiLv-MSFT
Instructions are clear as day. I'll definitely give this a go and get back to you!

0 Votes 0 ·

@ZhiLv-MSFT Worked perfectly! Thanks so much!

Just a few things I learned along the way, I ended up using the first way and couldn't get the JS to work properly to fire the method.

I learned that I couldn't use an IEnumerable as the model for the page as for some reason it wouldn't transfer the data across and it also wouldn't let me use a for loop, unless it was just a List<Object>.

0 Votes 0 ·