My controller
public ActionResult Edit(int id)
{
return this.EditDefault(id);
}
[HttpPost]
public ActionResult Edit(int id, Models.Company model)
{
return this.EditDefault(id, model);
}
My model
pulbic class Company
{
... Many other Propeties
public HttpPostedFileBase File { get; set; }
}
My view
@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
{
... Many other Properties
@Html.TextBoxFor(m => m.File, new
{
type = "file", style = "display:none"
})
... Submit
}
So now my problem is that when I submit the page, the information in the model is correct, but the file property is still zero.
I found some solutions in which people added HttpPostedFileBase as a parameter in the controller (I tried it also does not work), but I would like to avoid this, since the model and controller are generated using T4. So is there someone why the file property is always null?
Would be very happy for some help :)
Update: Find a thanks solution for Matt Tabor.
, .
javascript , , .
@{
RouteData routeData = this.ViewContext.RouteData;
string currentController = routeData.GetRequiredString("controller");
}
@using (Html.BeginForm("Edit", currentController, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
... Many other Properties
@Html.TextBoxFor(m => m.File, new
{
type = "file", style = "display:none"
})
<span id="fake-file-name">Kein Bild</span>
... Submit
}
<script type="text/javascript">
$(function () {
$("#fake-file-name").click(function () {
$("#File").click();
});
$("#File").bind('change', function () {
var displayFileName = this.value.replace("C:\\fakepath\\", "");
$("#fake-file-name").text(displayFileName);
});
});
</script>