0

i've encountered some issue where i cannot get the file from the form with ajaxOptions. Below are the code..

@using (Ajax.BeginForm(null, null, new AjaxOptions { OnBegin = "blockUI", OnSuccess = "handleFormSuccess", OnFailure = "onAjaxFailure" }, new { enctype = "multipart/form-data" }))
{
<div class="col-md-4">
                            <div class="form-group">
                                @Html.LabelFor(model => model.MediaName, new { @class = "control-label" })
                                <span class="text-danger" aria-required="true"> * </span>
                                @Html.TextBoxFor(model => model.MediaName, new { @class = "form-control", @placeholder = LocalizationViewModel.Media.MediaName })
                                <span class="text-danger">@Html.ValidationMessageFor(model => model.MediaName)</span>
                            </div>
                        </div>
<div class="row col-md-12">
                        <div id="imageContent" class="form-group">
                            @Html.Label(@LocalizationViewModel.Media.Image, new { @class = "control-label" })
                            <div class="col-md-12">
                                @Html.TextBoxFor(model => model.MediaFile, new { type = "file" })
                            </div>
                        </div>
                    </div>
    }

if i change to this, it's working file.

@using (Html.BeginForm("CreateMedia", "Media", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data" }))
{
<div class="col-md-4">
                            <div class="form-group">
                                @Html.LabelFor(model => model.MediaName, new { @class = "control-label" })
                                <span class="text-danger" aria-required="true"> * </span>
                                @Html.TextBoxFor(model => model.MediaName, new { @class = "form-control", @placeholder = LocalizationViewModel.Media.MediaName })
                                <span class="text-danger">@Html.ValidationMessageFor(model => model.MediaName)</span>
                            </div>
                        </div>
        <div id="imageContent" class="form-group">
                            @Html.Label(@LocalizationViewModel.Media.Image, new { @class = "control-label" })
                            <div class="col-md-12">
                                @Html.TextBoxFor(model => model.MediaFile, new { type = "file" })
                            </div>
                        </div>
                    </div>
}

below are my controller and view model.

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> CreateMedia(CreateMediaViewModel viewModel)
        { // some code here
}


public class CreateMediaViewModel
    {
[Display(ResourceType = typeof(Media), Name = "MediaName")]
        [Required(ErrorMessageResourceType = typeof(Message), ErrorMessageResourceName = "MessageFieldRequired")]
        public string MediaName { get; set; }

        [Display(ResourceType = typeof(Media), Name = "Image")]
        public HttpPostedFileBase MediaFile { get; set; }
    }

Have anyone have the idea to make it works? :( i been stuck here for some times...thanks..

Darren Lau
  • 1,995
  • 4
  • 25
  • 40
  • 2
    You need to use `FormData` i you want to post a file using ajax. Refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Nov 16 '15 at 11:57
  • I've used your sample with a new MVC 5 project...and it is working well. Are you sure you pasted everything? Or maybe the issue is in MVC 3/4 projects only? – Kryszal Nov 16 '15 at 12:25

2 Answers2

0

In the second case, you are explicitly declaring action and controller's names:

Html.BeginForm("Edit", "ClientProgrammeProduct",...

Try the same with Ajax.BeginForm (instead using nulls).

Look here too: How to do a ASP.NET MVC Ajax form post with multipart/form-data?

Community
  • 1
  • 1
Kryszal
  • 1,663
  • 14
  • 20
0

I added this code on my script section and my file is now detected on my HttpPostedFileBase parameter on my controller.

<script>
window.addEventListener("submit", function (e) {
var form = e.target;
if (form.getAttribute("enctype") === "multipart/form-data") {
    if (form.dataset.ajax) {
        e.preventDefault();
        e.stopImmediatePropagation();
        var xhr = new XMLHttpRequest();
        xhr.open(form.method, form.action);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                if (form.dataset.ajaxUpdate) {
                    var updateTarget = document.querySelector(form.dataset.ajaxUpdate);
                    if (updateTarget) {
                        updateTarget.innerHTML = xhr.responseText;
                    } 
                }
            }
        };
        xhr.send(new FormData(form));
    }
}
}, true);
</script>
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57