2016-08-01 75 views
0

我有一個ajax函數的問題。我想發送param到控制器上的方法和這個ajax函數調用方法兩次。MVC Ajax函數調用兩次

AJAX( 「EXP」)。

$(document).ready(function() { 


    $(document).on('click', '.exp', function (e) { 

     var st_date = $(this).parent().find('.start').val(); 
     var ed_date = $(this).parent().find('.end').val(); 

     $.ajax({ 


      url: '/Reports/Report_CLeav/', 

      data: { 
       start_date:st_date, 
       end_date:ed_date 
       } 
     }).success(function (data) { 



      }) 

    }); 

}) 
+1

嘗試首先分離處理程序,比如'$(文件).off( '點擊')。在('click'' – Yang

+0

您能不能告訴我們'.exp'元素? –

+0

Html.ActionLink(「Export Report」,「Report_CLeav」,「Reports」,new {class =「IndexButton exp」,@style =「text-decoration:none; color:white」}) – POL11

回答

0

$點擊(函數(){

 var st_date = $(this).parent().find('.start').val(); 
     var ed_date = $(this).parent().find('.end').val(); 

     $.ajax({ 


      url: '/Reports/Report_CLeav/', 

      data: { 
       start_date:st_date, 
       end_date:ed_date 
       } 
     }).success(function (data) { 






     }) 

    }); 

0
<th> 
        Start date: @Html.TextBox("start_date", null, new { @class = "dateClass start", id = "StartDate" }) 
        End date: @Html.TextBox("end_date", null, new { @class = "dateClass end", id = "EndDate", @data_toggle = "popover", @data_content = "End date should be greater than Start date. ", @title = "Attention" }) 

        @Html.ActionLink("Export Report", "Report_CLeav", "Reports", new { @class = "IndexButton exp", @style = "text-decoration: none;color:white" }) 
       </th> 

「控制器」 公共類? ReportsController:控制器 { // GET:導出 public ActionResult索引() { return View(); }

公共無效Report_CLeav(DateTime的起始日期,日期時間END_DATE?) { 串路徑= HttpContext.Server.MapPath( 「〜/ App_Data文件/報告/ Report_LeavingCompanyHCT.xlsx」); Models.Report.Report_CompLeav reportcompleav = new Models.Report.Report_CompLeav(); var fileinfo = new FileInfo(path);

 using (ExcelPackage package = new ExcelPackage(fileinfo)) 
     { 

      var currentWorksheet = package.Workbook.Worksheets["HC"]; 

      using (var excelToExport = new ExcelPackage()) 
      { 
       excelToExport.Workbook.Worksheets.Add(currentWorksheet.Name, currentWorksheet); 
       var workBook = excelToExport.Workbook.Worksheets["HC"]; 
       try 
       { 
        workBook = reportcompleav.exportAllEmployeeDataRRecords(workBook,start_date,end_date); 
       } 
       catch (Exception e) 
       { 
        ViewBag.IsError = true; 
       } 


       excelToExport.Save(); 

       Stream stream = excelToExport.Stream; 
       var memoryStream = stream as MemoryStream; 

       Response.Clear(); 
       Response.ContentType = "application/vnd.openxmlformats"; 
       Response.AddHeader("Content-Disposition", 
            "attachment; filename=" + fileinfo.Name); 
       Response.BinaryWrite(memoryStream.ToArray()); 

      } 
     } 

    } 
} 

}