I have a contact form in a page that looks like this:
@model LessonsLearned.Domain.Common.ViewModels.ContactFormViewModel
@using (Html.BeginForm("SubmitForm", "Help", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<p>@LLResources.GetResourceText("Question")</p>
<br />
<div class="row form-group">
<div class="col-12">
<b>@LLResources.GetResourceText("Admin_Email")</b>
</div>
<div class="col-12">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
</div>
</div>
<div class="row form-group">
<div class="col-12">
<b>@LLResources.GetResourceText("General_Name")</b>
</div>
<div class="col-12">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
</div>
</div>
<div class="row form-group">
<div class="col-12">
<b>@LLResources.GetResourceText("Admin_SchoolName")</b>
</div>
<div class="col-12">
@Html.EditorFor(model => model.SchoolName, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
</div>
</div>
<div class="row form-group">
<div class="col-12">
<b>@LLResources.GetResourceText("Message")</b>
</div>
<div class="col-12">
@Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
</div>
</div>
<div class="row form-group">
<div class="col-12">
<b>@LLResources.GetResourceText("Observation_Attachments")</b>
</div>
<div class="col-12">
@Html.TextBoxFor(m => m.File, new { type = "file" })
@Html.ValidationMessageFor(m => m.File)
</div>
</div>
<div class="row">
<div class="col-12">
<input type="submit" value="Submit" class="btn btn-success float-md-right mr-md-2" />
</div>
</div>
}
The model behind the page looks like this:
public class ContactFormViewModel
{
[Required(ErrorMessage = "Email address is required.")]
[EmailAddress(ErrorMessage = "Must be a valid email address.")]
public string Email { get; set; }
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }
[Required(ErrorMessage = "School name is required.")]
public string SchoolName { get; set; }
[Required(ErrorMessage = "Message is required.")]
[DataType(DataType.MultilineText)]
public string Message { get; set; }
[FileExtensions(Extensions = "jpg, jpeg, png")]
public HttpPostedFileBase File { get; set; }
}
And then finally in my controller I have:
[HttpPost]
public ActionResult SubmitForm(ContactFormViewModel contactFormViewModel)
{
if (ModelState.IsValid)
{
//Do stuff
ViewBag.result = "Form submitted successfully!";
return View("Index", new ContactFormViewModel());
}
else
{
//Don't do stuff
ViewBag.result = "Form was not submitted, please fix errors.";
return View("Index", contactFormViewModel);
}
return RedirectToAction("Index");
}
For some reason when I upload a jpg on png to the form and try and submit, ModelState keeps telling me that there is an error - "The File field only accepts files with the following extensions: .jpg, .jpeg, .png". It's definitely a jpg or png I'm uploading. Why does it not recognise that?