2012-07-18 70 views
1
<div id="ProductionReport"> 
    <div class="ui-widget"> 
     @Using Ajax.BeginForm("Index", "Reports", ajaxOptions:=New AjaxOptions With {.InsertionMode = InsertionMode.Replace, .HttpMethod = "POST", .UpdateTargetId = "searchresultdata"}, htmlAttributes:=New With {.id = "productionform"}) 
      @Html.ValidationSummary(True) 
      @<fieldset class="ui-widget-content"> 
       <legend class="ui-state-default">Production Report</legend> 
       <div class="editor-label"> 
        @Html.LabelFor(Function(model) model.ProductionReportData.FromDate, "From Date: ") 
        @Html.Raw(" ") 
        @Html.TextBoxFor(Function(model) model.ProductionReportData.FromDate, New With {.class = "ui-state-default", .id = "datepicker", .Style = "font-size:x-small;", .name = "fromdate"}) 
        @Html.ValidationMessageFor(Function(model) model.ProductionReportData.FromDate) 
       </div> 
       <div class="editor-label"> 
        @Html.LabelFor(Function(model) model.ProductionReportData.ToDate, "To Date: ") 
        @Html.Raw(" ") 
        @Html.TextBoxFor(Function(model) model.ProductionReportData.ToDate, New With {.class = "ui-state-default", .id = "datepicker2"}) 
        @Html.ValidationMessageFor(Function(model) model.ProductionReportData.ToDate) 
       </div> 
       <div class="editor-label"> 
        @Html.LabelFor(Function(model) model.ProductionReportData.ShowDetails, "Show Details: ") 
        @Html.EditorFor(Function(model) model.ProductionReportData.ShowDetails) 
       </div> 
       <div class="editor-label"> 
        <input class="ui-button ui-button-text-only ui-widget ui-state-default ui-corner-all" 
         type="submit" value="Search" name="btnSearch" /> 
        <input class="ui-button ui-button-text-only ui-widget ui-state-default ui-corner-all" 
         type="submit" value="Export Data" name="btnSearch" /> 
       </div> 
      </fieldset> 
     End Using 
    </div> 
    <div id="searchresultdata" class="ui-widget-content"> 
    </div> 
</div> 

enter code h<HttpPost()> _ 
    Function ExportProductionReportData(ByVal data As ReportsModel) As FileResult 


     Dim var = From I In db.scll_label Join P In db.pt_mstr On I.scll_part Equals P.pt_part _ 
        Join C In db.sclws_cfg On I.scll_wsid Equals C.sclws_id _ 
        Where P.pt_domain = "mueller" And C.sclws_domain = "mueller" _ 
        And I.scll_transactiondate >= data.ProductionReportData.FromDate And I.scll_transactiondate <= data.ProductionReportData.ToDate _ 
        Select I, P.pt_length, P.pt_net_wt, P.pt_desc1, P.pt_tol_high, P.pt_part_type, P.pt_um, _ 
        C.sclws_proj_code, C.sclws_site 

     Dim ProMod As New ProductionReportModel 
     ProMod.ProductionDetails = New ObservableCollection(Of ProductionReportDetails) 
     ProMod.ProductionGrandTotal = New ProductionReportGrandTotals 
     ProMod.ProductionPartTotals = New ObservableCollection(Of ProductionReportTotalsByPart) 


     ''Generate Production Details 
     For Each rec In var 

      Dim proModDtls As New ProductionReportDetails 

      proModDtls.Customer = rec.I.scll_cust 
      proModDtls.Feet = rec.I.scll_total_feet 
      proModDtls.GrossWeight = rec.I.scll_weight 
      proModDtls.NetWeight = rec.I.scll_weight - rec.I.scll_total_tare 
      proModDtls.Part = rec.I.scll_part 

      If rec.pt_um = "LB" Then 
       Try 
        proModDtls.Pieces = rec.I.scll_total_feet/rec.pt_length 
       Catch ex As Exception 
        proModDtls.Pieces = rec.I.scll_qty 
       End Try 
      ElseIf rec.pt_um = "FT" Then 
       Try 
        proModDtls.Pieces = rec.I.scll_qty/rec.pt_length 
       Catch ex As Exception 
        proModDtls.Pieces = rec.I.scll_qty 
       End Try 
      Else 
       proModDtls.Pieces = rec.I.scll_qty 
      End If 

      proModDtls.Quantity = rec.I.scll_qty 

      proModDtls.ReasonCode = rec.sclws_proj_code 

      proModDtls.StandardWeight = rec.I.scll_std_weight 

      proModDtls.Station = rec.I.scll_wsid 

      proModDtls.Tare = rec.I.scll_total_tare 

      proModDtls.TareDetail = rec.I.scll_tare_detail 

      proModDtls.Ticket = rec.I.scll_ticket 

      Try 
       proModDtls.DrawingEfficiency = ((rec.I.scll_weight - rec.I.scll_total_tare)/rec.I.scll_std_weight) * 100 

      Catch ex As Exception 

      End Try 

      proModDtls.TicketDate = rec.I.scll_transactiondate 

      ProMod.ProductionDetails.Add(proModDtls) 

     Next 
     Dim csvCreator As New CSVFileMaker(Of ProductionReportDetails, List(Of ProductionReportDetails)) 
     csvCreator.data = ProMod.ProductionDetails.ToList 
     Dim csvdata As String = csvCreator.GenerateCSVFile 
     Dim d = Encoding.ASCII.GetBytes(csvdata) 
     Dim filename As String = "ProductionReport" & Date.Now.Year.ToString & "_" & Date.Now.Month.ToString & "_" & Date.Now.Day & ".csv" 
     Dim cd = New System.Net.Mime.ContentDisposition With {.FileName = filename, .Inline = False, .DispositionType = "attachment"} 
     Response.AppendHeader("Content-Disposition", cd.ToString) 
     Return File(d, "text/csv", filename) 




    End Function 

'GET:/報告 _ 功能指數(BYVAL btnSearch作爲字符串,BYVAL數據作爲ReportsModel)作爲的ActionResultMVC3 Ajax.BeginForm下載後文件

 data.DrawingEfficiencyWeeklyData = New DrawingEfficiencyWeeklyData 
     data.DrawingEfficiencyPeriodData = New DrawingEfficiencyPeriodData 
     data.DrawingEfficiencyYearlyData = New DrawingEfficiencyYearlyData 
     data.DrawingEfficiencyYearlyMeanData = New DrawingEfficiencyYearlyMeanData 
     data.DrawingEfficiencySummaryData = New DrawingEfficiencySummaryData 
     data.ToleranceOverrideReportData = New ToleranceOverrideReportData 
     data.TareDetailReportData = New TareDetailReportData 

     Dim var = db.GetGLCYears 
     Dim var2 = db.GetGLCYears 
     Dim var3 = db.GetGLCYears 
     Dim var4 = db.GetGLCYears 

     Dim allList As New SelectListItem With {.Value = 0, .Text = "All"} 
     Dim BonusList As New SelectListItem With {.Value = 1, .Text = "Bonus"} 
     Dim OtherList As New SelectListItem With {.Value = 2, .Text = "Other"} 

     Dim categoryList As New List(Of SelectListItem) 
     categoryList.Add(allList) 
     categoryList.Add(BonusList) 
     categoryList.Add(OtherList) 

     data.DrawingEfficiencyYearlyMeanData.CategoryList = categoryList 
     data.DrawingEfficiencyYearlyMeanData.YearList = var4.Select(Function(i) New SelectListItem With {.Value = i.Y, .Text = i.Y}) 
     data.DrawingEfficiencyWeeklyData.YearList = var.Select(Function(i) New SelectListItem With {.Value = i.Y, .Text = i.Y}) 
     data.DrawingEfficiencyPeriodData.YearList = var2.Select(Function(i) New SelectListItem With {.Value = i.Y, .Text = i.Y}) 
     data.DrawingEfficiencyYearlyData.YearList = var3.Select(Function(i) New SelectListItem With {.Value = i.Y, .Text = i.Y}) 

     Select Case btnSearch 

      Case Is = "Search" 
       Return (SearchProductionReport(data)) 
      Case Is = "Export Data" 
       Return ExportProductionReportData(data) 
      Case Else 
       Return View(data) 
     End Select 

     Return View(data) 
    End Function 

基本上我的形式發送Ajax響應分區,其中包含部分視圖,但我有另一個提交按鈕,使用相同的數據來生成一個CSV文件。我無法得到這個文件下載它只是輸出到div。任何人都可以幫助或引導正確的方向..我已經嘗試actionlinks,但它永遠不會堅持我的模型到數據庫我不能使用html.beginforms,因爲我不能設置一個目標div加載部分視圖到。

更新:使用腳本,但仍然沒有工作

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#productionsearch").click(function() { 
     $("#productionform").submit(function() { 
      $.ajax({ 
       url: this.action, 
       type: this.action, 
       data: $(this).serialize(), 
       success: function (result) { 
       $("#searchresultdata").html(result); 
       } 
      }); 
     }); 
    }); 
}); 

`

回答

1

不能使用AJAX來下載文件。只需使用正常的Html.BeginForm即可。

+0

所以,我怎麼可以更新的結果DIV當我需要顯示的局部視圖。我有兩個按鈕之一,它運行查詢並返回呈現爲div的部分視圖,第二個按鈕允許您使用csv下載文件。是否有另一種方式,我可以獲得與HTML.BeginForm相同的功能在Ajax.Begin ajaxoptions中的示例您有一個updatetargetid我可以用HTML.BeginForm實現此功能 – Jdsurgeon 2012-07-19 13:04:01

+0

當您下載文件時,系統會提示用戶保存他電腦上的文件。該視圖不會刷新。 – 2012-07-19 13:10:13

+0

我想我可能沒有正確解釋這一點。我需要表單中的值來生成視圖或下載。我不想爲了顯示視圖而去新的頁面。它在結果下面填寫了一個div。我只是希望能夠在div中填充視圖並獲取表單數據並創建一個csv下載文件。這在aspx時代非常簡單。但我不能使用mvc3 ... – Jdsurgeon 2012-07-19 13:17:06

0
<script type="text/javascript"> 
$(document).ready(function() { 
    $("#ProductionSearch").click(function() { 
     alert("Button Clicked"); 
     var $form = $("#ProductionSearch").parents('form') 
     $.ajax({ 
      type: "POST", 
      url: "@URL.Action("SearchProductionReport")", 
      data: $form.serialize(), 
      error: function (xhr, status, error) { 
       alert("an error occurred" + error) 
      }, 
      success: function (response) { 
       alert("Function worked"); 
       $("#searchresultdata").html(response); 
      } 
     }); 
    }); 
}); 

答案。我想到了。使用HTML.BeginForm(「YourDownloadAction」,「YourController」,「表單ID」) 然後創建兩個類型爲submit的按鈕,另一個類型爲button。確保你的javascript正在調用不需要下載文件的操作。就是這樣。現在你必須實現驗證,因爲它不會通過jquery在ajax調用中實現......感謝Darin你至少指出了我正確的方向。

更新了驗證

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#ProductionSearch").click(function() { 
     var $form = $("#ProductionSearch").parents('form') 
     if ($($form).validate().form() == true) { 
     $.ajax({ 
      type: "POST", 
      url: "@Url.Action("SearchProductionReport", "Reports", Nothing)", 
      data: $form.serialize(), 
      error: function (xhr, status, error) { 
       $("#dialog").dialog("open"); 
       $("#dialog").html("<p>Please be advised that an error has occured while searching production data please try again or contact your administrator</p> " +"<br>" + error + "</br>"); 
      }, 
      success: function (response) { 
       $("#searchresultdata").html(response); 
      } 
     }); 
     } 
    }); 
}); 

+0

這裏是腳本驗證,如果有人需要它,但你的模型必須dataannotated。 – Jdsurgeon 2012-07-19 17:44:15