Ich habe dieses Problem, wenn ich versuche, ein Bild hochzuladen. Wenn ich den Absenden-Button klicken, erhalte ich folgende Fehlermeldung: clickBild hochladen Fehler in ASP MVC
wenn ich withaut modal param es funktioniert, aber das Fenster ist nicht in modal und sieht sehr schlecht.
Modell:
public partial class Prod
{
public string PName { get; set; }
public byte[] PFile{ get; set; }
}
Übersicht:
@using (Html.BeginForm("Create", "Products", FormMethod.Post, new { enctype = "multipart/form-data" })){
@Html.ActionLink(" ", "Create", "Products", null, new { data_modal = "file", id = "btnCreate", @class = "btn btn-small btn-primary pull-right fa fa-cart-plus" })
@Html.ActionLink(" ", "Create", "Products", null, new { id = "btnCreate", @class = "btn btn-small btn-primary pull-right fa fa-cart-plus" })
}
Ansicht erstellen:
@using (Html.BeginForm("Create", "Products", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PFile, "Plik", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.TextBoxFor(model => model.PFile, new { type = "file", name = "imageF" })
@Html.ValidationMessageFor(model => model.PFile, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal">Anuluj</button>
<input class="btn btn-primary" type="submit" value="Zapisz" />
</div>
}
Controller:
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Exclude = "PFile")] Prod pro, HttpPostedFileBase imageF)
{
if (ModelState.IsValid)
{
if (imageF != null)
{
pro.PName= imageF.ContentType;
pro.PFile= new byte[imageF.ContentLength];
imageF.InputStream.Read(pro.PFile, 0, imageF.ContentLength);
}
db.Prods.Add(pro);
db.SaveChanges();
return Json(new { success = true });
}
return PartialView("Create", pro);
}
modalform.js
$(function() {
$.ajaxSetup({ cache: false }); $("a[data-modal]").on("click", function (e) { // hide dropdown if any $(e.target).closest('.btn-group').children('.dropdown-toggle').dropdown('toggle'); $('#myModalContent').load(this.href, function() { $('#myModal').modal({ /*backdrop: 'static',*/ keyboard: true }, 'show'); bindForm(this); }); return false; });
});
function bindForm(dialog) {
$('form', dialog).submit(function() { $.ajax({ url: this.action, type: this.method, data: $(this).serialize(), contentType: 'multipart/form-data', success: function (result) { if (result.success) { $('#myModal').modal('hide'); //Refresh location.reload(); } else { $('#myModalContent').html(result); bindForm(); } } }); return false; }); }
Bitte helfen dieses Problem zu lösen.
Hat die Formcodierung Typ verloren gehen, wenn Sie durch Ajax schreiben? –
Entschuldigung, ich bin Begginer und ich weiß nicht, was Sie das meinen – Ebasse
Ich meine, dass Sie enctype multipart/form-data zum Formular hinzugefügt, aber dann reichen Sie Ihre Datei durch eine Ajax-Post stattdessen, und ich sah nicht, dass das Tragen derselben Codiertyp. –