Debashish Dwivedi Debashish Dwivedi - 1 month ago 6
C# Question

System.ArgumentNullException: Value cannot be null. Parameter name: source

here my code is its showing the parameter you have passed is null:-

[HttpPost]
public String Indexhome( IEnumerable<Seat> Seats )
{
if (Seats.Count(x => x.IsSelected) == 0)
{
return "you didnt select any seats";
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append("you selected");
foreach (Seat seat in Seats)
{
if (seat.IsSelected)
{
sb.Append(seat.Name + " ,");
}
}
sb.Remove(sb.ToString().LastIndexOf(","), 1);
return sb.ToString();
}
}

Answer

Seats will be null if you call the method without matching data / query argument. You need to also check that, like so for instance:

[HttpPost]
public String Indexhome( IEnumerable<Seat>  Seats )
{
     if ((Seats == null) || !Seats.Any(s => s.IsSelected))
     {
            return "you didnt select any seats";
     }
     else
     {
           return "you selected " + string.Join(", ", Seats.Where(s => s.IsSelected).Select(s => s.Name));
     }   
}