So I'm fairly new to ASP.net MVC and have been trying to make a fileuploader, but I can't seem to get my uploaded file bound to my viewmodel. I'm trying to apply validation to the uploaded file through my viewmodel which should be doable.
My code:
View:
<div id = "PDFForm">
@using (Ajax.BeginForm("Upload", "Home", FormMethod.Post,
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "PDFForm",
},
new { id = "UploadForm", enctype = "multipart/form-data" }))
{
<input type="file" name="Cover" id="Cover" />
<input type="submit" value="Upload"/>
}
</div>
ViewModel:
public class UploadVM
{
[Required]
public HttpPostedFileBase Cover { get; set; }
}
Controlller action:
public ActionResult Upload(UploadVM model)
{
if(ModelState.IsValid() && model.Cover !=null)
{
//do things and return a response view
}
else
{
//return to form
}
}
I've googled around for tutorials and they seem to be able to bind the uploaded file to the viewmodel by using:
enctype = "multipart/form-data"
I can't seem to get it working so I thought you guys might be able to help me out or push me in the right direction.
Thanks in advance!
Edit: I've tried it with HTML.BeginForm() too but that doesn't seem to work either