lundi 16 novembre 2015

MVC HttpPostedFileBase always null with ajax options

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..

Aucun commentaire:

Enregistrer un commentaire