通过设置src of element将模型发送到MVC3控制器

Send a model to MVC3 controller by setting src of element

本文关键字:MVC3 控制器 模型 设置 src of element      更新时间:2023-09-26

我试图触发Excel文档的下载。我的控制器返回可下载文件:

public ActionResult ExportPatchSchedules([Bind(Prefix = "multiSelectDialog")] ExportPatchSchedulesModel model)
{
    MemoryStream memoryStream = WorkflowManager.ExportPatchScheduleReport(model.TaskIDs);
    return File(memoryStream.ToArray(), "application/vnd.ms-excel",
        string.Format(@"Patch Schedule Report {0}.xls", string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now)));
}

为了触发下载,我有以下内容:

$('<iframe>', {
    src: '../Workflow/ExportPatchSchedules/',
    css: {
        display: 'none'
    }
}).appendTo('body');

然而,在此之前,我使用AJAX请求来访问我的控制器方法:

$.ajax({
    type: "POST",
    url: '../Workflow/ExportPatchSchedules',
    data: formData
});

其中formData是exportpatchschedulemodel的序列化表示。

当我被限制设置src时,我正在努力将我的模型发送到ExportPatchSchedules。可以用这种方式把我的模型送过去吗?如果不是. .这通常是怎么做的?

尝试使用html form与它的target属性设置为iframename,如:

<iframe style="display: none;" id="myIframe" name="myIframe"></iframe>
var f = document.createElement("form");
f.method = "POST";
f.action = "../Workflow/ExportPatchSchedules";
f.target = "myIframe";
f.enctype = "application/x-www-form-urlencoded"; // not sure about this since you didn't mention
var input = document.createElement('input');
input.type = "hidden";
input.name = "model";
input.value = "...." // your data here, not sure about it since you didn't mention
f.appendChild(input);
f.submit();