Trying to upload a file with mvc4 and im getting null returned in my controller for the file upload. Can anyone see why please?
View Model
public class PostExtendedWithImage
{
public Post post { get; set; }
public HttpPostedFileBase file { get; set; }
}
View
@using (Ajax.BeginForm("CreatePost", "Wall", new AjaxOptions
{
HttpMethod = "post",
InsertionMode = System.Web.Mvc.Ajax.InsertionMode.InsertBefore,
UpdateTargetId = "newPost"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Post</legend>
<div class="editor-label">
</div>
<div class="editor-field">
@Html.HiddenFor(model => model.post.Username, new { Value = User.Identity.Name })
</div>
@Html.FileFor(model => model.file)
<div class="editor-label">
@Html.LabelFor(model => model.post.PostContent)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.post.PostContent)
@Html.ValidationMessageFor(model => model.post.PostContent)
</div>
@{
TempData["returnURL"] = Request.Url.AbsoluteUri;
}
<p>
<input type="submit" id="postStatus" value="Create" />
</p>
</fieldset>
}
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult CreatePost(PostExtendedWithImage VM)
{
if (ModelState.IsValid)
{
if (VM.file == null)
{
// Do stuff
}
else
{
//Save file to DB
}
return PartialView("_NewStatusPartial",VM);
}
return PartialView("All",VM);
}