Request.Files.Count immer 0 beim Hochladen von Bildern in MVC 5
Ich habe einen Post-Controller für ein Modell mit einigen Zeichenfolgenfeldern und einem Bild. Genau identischer Code funktioniert auf MVC4, aber in MVC 5 ist der Request.Files.Count immer 0
Mein Modell hat Byte [] für Bild anstelle von HttpPostedFileBase
Meine Sicht:
@using (Html.BeginForm("Create", "HotelManager", FormMethod.Post, new { enctype = "multipart/form-data", data_ajax = "false" }))
{
@Html.AntiForgeryToken()
@Html.TextBoxFor(model => model.Title, new { @class = "form-control" })
@Html.TextAreaFor(model => model.Description, new { @class = "form-control", @rows = "7" })
<input type="file" class="form-control filestyle">
<input type="submit" value="Submit" class="btn btn-block" />
}
Ich habe versucht, data_ajax = "false" wegzulassen, aber keine Verwendung.
Mein Post-Controller sieht wie folgt aus:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Title,Description,Image")] Hotel hotel)
{
if (ModelState.IsValid)
{
// deal with the uploaded file
if (Request.Files.Count > 0)
{
HttpPostedFileBase file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
// Code to process image, resize, etc goes here
}
}
// Other code to add the hotel to db goes here
}
Im Debugger sehe ich, dass der Request.Files.Count immer Null ist, ich weiß nicht warum? Ich habe sowohl den Ansichts- als auch den Controller-Code aus einem anderen MVC4-Projekt kopiert, in dem es einwandfrei funktioniert.
Key Value
Request POST /HotelManager/Create HTTP/1.1
Accept text/html, application/xhtml+xml, */*
Referer http://localhost:4976/HotelManager/Create
Accept-Language en-US,en;q=0.5
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; ASU2JS; rv:11.0) like Gecko
Content-Type multipart/form-data; boundary=---------------------------7de207070a24
Accept-Encoding gzip, deflate
Host localhost:4976
Content-Length 946
DNT 1
Und im IE-Entwicklerfenster sehe ich, dass der Formulartext leer ist.