繁体   English   中英

通过设置元素的src将模型发送到MVC3控制器

[英]Send a model to MVC3 controller by setting src of element

我正在尝试触发下载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是ExportPatchSchedulesModel的序列化表示形式。

当我被限制设置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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM