Pass IEnumerable<Object> to Controller Method

Adrian N 81 Reputation points
2021-04-15T05:13:26.31+00:00

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?

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-04-15T09:09:48.71+00:00

    Hi @Adrian N ,

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

    1. Use asp.net 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++)  
              {  
                  <div>@Model[i].QuestionTitle</div>  
                  <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">     
          </form>   
      
      Controller:
          [HttpPost]  
          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. 88138-image.png 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:

    88114-2.gif

    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,
    Dillion


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.