MVC HttpPostedFileBase在使用ajax选项时始终为null

本文关键字:null 选项 ajax HttpPostedFileBase MVC | 更新日期: 2023-09-27 18:22:14

我遇到了一些问题,无法使用ajaxOptions从表单中获取文件。以下是代码。。

@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>
    }

如果我改成这个,它就是工作文件。

@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>
}

下面是我的控制器和视图模型。

[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; }
    }

有人有让它发挥作用的想法吗?:(我被困在这里好几次了……谢谢。

MVC HttpPostedFileBase在使用ajax选项时始终为null

在第二种情况下,您显式地声明操作和控制器的名称:

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

Ajax.BeginForm尝试同样的操作(而不是使用nulls)。

看看这里:如何使用多部分/表单数据进行ASP.NET MVC Ajax表单发布?

我在脚本部分添加了这段代码,现在在控制器上的HttpPostedFileBase参数上检测到了我的文件。

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