我已使用以下 jQuery Ajax 代码将视图中的模型传递给控制器:
$.ajax({
data: model,
type: "POST",
url: '@Url.Action("createDoc")',
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
alert('Done '+ result.toString());
}
});
问题是:在Controller方法“createDoc”
[HttpPost]
public ActionResult createDoc(IEnumerable<Movie> movies)
{
CreateWordprocessingDocument(movies);
return Json(new { result = movies.Count()});
}
我无法对电影 Enumerable 数据做任何事情。调用 CreateWordProcessingDocument 创建一个包含电影数据的文档。但事实并非如此。这是方法代码:
public void CreateWordprocessingDocument(IEnumerable<Movie> movies)
{
HttpContextWrapper context = new HttpContextWrapper(System.Web.HttpContext.Current);
context.Response.Clear();
context.Response.Buffer = true;
context.Response.AddHeader("content-disposition", "attachment;filename=example.doc");
context.Response.ContentType = "application/vnd.ms-word.document";
context.Response.Charset = "";
StringBuilder sb = new StringBuilder();
sb.AppendLine("<p align='Center'><b> GENERAL TITLE</b></p>");
sb.Append("<br>"+movies.Count());
for (int i = 0; i < movies.Count(); i++) {
sb.Append("<br>Title:" + movies.ElementAt(i).Title +"");
}
context.Response.Output.Write(sb.ToString());
context.Response.Flush();
context.Response.End();
}
但它不起作用:它返回到 ajax 帖子并弹出“完成”警报,result.tostring() 显示我在 createWordProcessingDocument 方法上创建的 HTML 代码。我怎样才能避免这种行为,以便我可以对从视图传递给控制器的数据做一些事情?谢谢。