Well, you can do this:
coll = coll ?? new List<string>();
Or you will need to implement a ModelBinder that will create an empty list instead of returning null. For instance:.
public EmptyListModelBinder<T> : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var model = base.BindModel(controllerContext, bindingContext) ?? new List<T>(); } }
And connected as:
ModelBinders.Binders.Add(typeof(IList<string>), new EmptyListModelBinder<string>());
I would probably stick with argument checking though ...
source share