Why after uploading a file, i used debugger and get always file's value null. type = file is used only once. binding is working. i used debugger and can see the value of file before and after the binding, always null
Here is my view
@model WebApplication1.Models.Post
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Add New Post</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Post</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PostTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostAuthor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostAuthor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostAuthor, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.WebSite, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.WebSite, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.WebSite, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.PostDate, new { @Value = @DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), @readonly = "readonly" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PostText, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PostText, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PostText, "", new { @class = "text-danger" })
</div>
</div>
@{
ViewBag.Title = "File Upload in MVC3 By Using Razor";
}
@using (Html.BeginForm("Create", "BlogController", FormMethod.Post , new { enctype = "multipart/form-data"}))
{
<div>
<b><u>File Upload in MVC3 By Using Razor</u></b>
Select Image
<input type="file" name="file" />
<input type="submit" value="Upload Image" name="Command" /><br />
</div>
<div>
@ViewBag.Message
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to Posts List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Here is my create action in the BlogController
public ActionResult Create([Bind(Include = "PostID,PostTitle,PostAuthor,WebSite,PostDate,PostText,PostImage,PostVideo")] Post post, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
file.SaveAs(HttpContext.Server.MapPath("~/Images/") + file.FileName);
post.PostImage = file.FileName;
return RedirectToAction("Index");
}
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}