Export to excel not working when using Ajax in MVC

Silent

New Member
Hi i have created export class that converts Gridview to excel file.See below code:DownloadFileActionResult Class:\[code\]public class DownloadFileActionResult : ActionResult { public GridView ExcelGridView { get; set; } public string fileName { get; set; } public DownloadFileActionResult(GridView gv, string pFileName) { ExcelGridView = gv; fileName = pFileName; } public override void ExecuteResult(ControllerContext context) { HttpContext curContext = HttpContext.Current; curContext.Response.ClearContent(); curContext.Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls"); curContext.Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); ExcelGridView.RenderControl(htw); curContext.Response.Write(sw.ToString()); curContext.Response.End(); } }\[/code\]Jquery-ajax:\[code\]function Export(){ var search = {}; search.Keyword = "MaterialShape"; var url_ = generateURL("/Home/Download"); //Call Save Controller and pass details entities $.ajax({ type: "POST", url: url_, data: search, //details will act as the Entities Model traditional: true, success: function(data) { }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("error: " + XMLHttpRequest.responseText); }, dataType: 'json' });};\[/code\]Search Parmeter Properties:\[code\]public class SearchParams { public string Keyword { get; set; } }\[/code\]And then i implement it on my controller:\[code\] //Export to excel public ActionResult Download(SearchParam param) { List<Lookup> lookupList = data.GetLookup(param.Keyword); var grid = new System.Web.UI.WebControls.GridView(); grid.DataSource = lookupList; grid.DataBind(); return new DownloadFileActionResult(grid, "test"); }\[/code\]It is working(without search param values) when i type the controller url manually\[code\]http://localhost:54928/Home/Download \[/code\]or using html.action link \[code\]<%= Html.ActionLink("Home", "/Download", "Home")%> \[/code\]but it is not working when i use ajax call\[code\]<img src="http://stackoverflow.com/questions/14050085/<%=Url.Content("~/Images/export.png")%>" id="Img1" onclick="Export();" alt="Export" /> \[/code\]that i really need to use.I am missing something here..any ideas?Thanks in Regards
 
Back
Top