Hi everyone I am creating an application using asp.net mvc with a codefirst database that allows the user to store an image along with some text in the database but I want the user to be able submit the text by itself with no images. The issue is that currently if the user submits the text without the image it crashs on this line if (file.ContentLength > 0) and gives a null reference exception. Thank you for anyhelp.
foreach (HttpPostedFileBase file in model.Files)
{
if (file.ContentLength > 0)
{
string fileName = file.FileName;
string Extension = Path.GetExtension(fileName);
string path = Path.Combine(Server.MapPath("~/Images/"), fileName);
file.SaveAs(path);
MyModel image = new MyModel()
{
Path = fileName,
FileName = fileName
};
post.Images.Add(image);
}
}
view
@Html.TextBoxFor(model => model.Files, new { type = "file", multiple = "multiple" })