I've tried Ajax.BeginForm, but does it not work.
With Ajax.BeginForm, the file arrives in null.
Now I'm trying to solve the problem with Html.BeginForm, the image also comes null.
What am I doing wrong?
_Upload.cshtml:
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Upload</h4>
</div>
@using (Html.BeginForm("Upload", "Account", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-xs-6">
@Html.LabelFor(model => model.Image)
@Html.TextBoxFor(model => model.Image, new { @class = "form-control", @type = "file" })
</div>
</div>
<center><div id="result"></div></center>
<div class="modal-footer">
<input type="submit" id="submit_button" class="btn btn-success" value="Update" />
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
<script>
$(function () {
$("#submit_button").click(function (e) {
e.preventDefault();
var _form = $(this).closest("form");
var _formData = _form.serialize();
$.post(_form.attr("action"), _formData, function (res) {
if (!res.IsSuccess) {
var errors = "";
$.each(res.Errors, function (i, item) {
errors = errors + "<li>" + item + "</li>";
});
$("#result").html('<div class="alert alert-success" role="alert"><strong>Well done!</strong> You successfully read <a href="#" class="alert-link">this important alert message</a>.</div>');
}
else {
//No errors. May be redirect ?
}
});
});
});
</script>
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload(My_Models model)
{
return Json(new { IsSuccess = false });
}
Removing JavaScript above the image arrive OK through model. Inseting the JavaScript above the image does not arrived. What is going on ?
The JavaScript above is only to show the message regarding to get image.