Anurag Anurag - 6 months ago 18
C# Question

Pass list of items from $.get() to MVC controller


controller Action method
looks like the below:

public Task<IActionResult> GetDetails(int id, string name, IEnumerable<Employee> employees)
//create a model
//Some business logic codes

return PartialView("_Partial.cshtml", model);

I need to call the above Action Method from
jQuery's $.get()
method on a button click, capture the partial view returned as
, and show it in a Bootstrap popup.

I am not able to pass the
from the
method, it is always
, whatever I try.

Below is the JS code:

<a class="btn btn-primary" onclick="ShowModal();" data-keyboard="true" data-toggle="modal">ShowModal</a>

<div class="modal fade" id="divShowModalDialog" role="dialog" tabindex="-1">
<div class="modal-body" id="divShowModalBody">

function ShowModal()
var list = [{ Id: 101, Gender: 'MALE' }, { Id: 102, Gender: 'FEMALE' }];
list = JSON.stringify(list);

var data = { 'id': 999, 'name': 'JAMES', 'employees': list };

$.get('/Area1/Controller1/ShowModal', data)
.done(function (response) {
if (response != undefined) {
backdrop: 'static',
keyboard: true,
.fail(function (xhr) {

I get the
parameter in the Action method, but the list is always empty. I have tried after removing
as well, but it doesn't work.
I know I'm missing a trivial thing, please help.


First, you should be using [HttpPost] on your controller action and not [HttpGet], and of course you'll need to use post from jQuery which is using $.post() and that is because 'POST' is the correct - but not the only - HTTP verb to actually post data to the server side.

Second, you shouldn't stringify your employees list before you put it in your data javascript object that you are sending. so, list = JSON.stringify(list); and just straight away go

var data = { 'id': 999, 'name': 'JAMES', 'employees': list };

You also might need to provide the dataType using $.post(url,data,onsucess,dataType) check documentation in the link above.

Last, on your action method remove IEnumerable<T> and replace it with a concrete collection type like List<T> because the JSON serializer will need to know which type of collection to instantiate at binding time.