philrabin
philrabin

Reputation: 809

List<type> parameter in asp mvc controller action returns null and not an empty list

I'm using ASP MVC 3 with jQuery.ajax to post array values with traditional:true. This works fine with an array that has values. My only problem is that if the array is empty in the js then the value passed into the respective parameter in the controller action is always null. How can I get the mvc to return an EMPTY list rather than NULL so that I don't have to check for null all over the place to use LINQ?

Javascript Code

$.ajax({
    url:"/Foo/BarAction",
    type:"POST",
    traditional:true,
    data:{myParam:[]}
}):

C# ASP MVC Code

public class FooController : Controller
{
        public ActionResult BarAction(List<string> myParam)
        {
            //myParam is null, and not an empty list
        }

}

Upvotes: 1

Views: 1887

Answers (1)

mccow002
mccow002

Reputation: 6914

To return an empty list, you need to at least initialize it. So, before you return it, you'll need:

myParams = new List<string>();

Upvotes: 1

Related Questions