I have object street which have some photos. I’m trying to implement upload on the same form as where creating new street object. So image a mvc web form with fields
- StreetName
- StreetNumber
- Photo1
- Photo2
- Photo3
Now, on the create.cshtml page I have form with StreetName and StreetNumber fields and
<input type="file" name="postedImages" />
<input type="file" name="postedImages" />
<input type="file" name="postedImages" />
On submiting this form I’m sending these data to the StreetController for further process
[HttpPost]
public ActionResult Create(StreetViewModel newData, IEnumerable<HttpPostedFileBase> postedImages)
{
//create object and save stretname and streetnumber
//process posted images
foreach(var image in postedImages)
{
//this is where error occured if I post only one or two images from my view
if(image.ContentLength >0)
{
//process and save images
}
}
}
As you can read inside my commented lines if I post exact number of images that are provided on the web form everything is fine, but If I send 1 or 2 images error occured.
How to solve this? Thanks
Add a
nullcheck before accessing theContentLengthproeprty