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 开发者_StackOverflow社区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
}
}
To return an empty list, you need to at least initialize it. So, before you return it, you'll need:
myParams = new List<string>();
精彩评论