2011-12-06 37 views
1

我有這樣一個觀點:MVC列表檢查和選擇操作,然後到CSV文件

@model IEnumerable<VectorCheck.Models.Invoice> 
@{ 
    ViewBag.Title = "Exportable Invoices"; 
} 
<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script> 
<script src="../../Scripts/jquery-ui-1.8.16.min.js" type="text/javascript"></script> 
<script src="../../Scripts/Views/Export/index.js" type="text/javascript"></script 
<header class="header"> 
    <div class="headerText"> 
     <h1>Exportable Invoices</h1> 
    </div> 
</header> 
@using (Html.BeginForm("Export", "Export")) { 
<table> 
    <tr class="mainheader"> 
     <th>Invoice Number</th> 
     <th>Date</th> 
     <th>Organisation</th> 
     <th>Total (Excl GST)</th> 
     <th>Status</th> 
     <th>Exported Date</th> 
     <th> 
      <select id="expenseSelect"></select> 
      <input type="submit" id="btnexport" value="Export" /> 
     </th> 
    </tr> 
    @foreach (var item in Model) { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.InvoiceNumber) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.InvoiceDate, "{0:D}") 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Organisation.Name) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.TotalExcludingGst) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Status) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.ExportedDateTime) 
      </td> 
      <td class="centered"> 
       <input type="checkbox" class="exportcheckbox" [email protected] /> 
      </td> 
     </tr> 
    } 
</table> 
} 
<div> 
    @Html.ActionLink("Back to Summary", "Index", "Invoice") 
</div> 

好了,看到每個複選框有着怎樣的attribrute [email protected]。那麼我試圖找到一個行動方法的所有發票,其複選框檢查的ID。此外,我試圖獲得選擇列表的費用選擇,其中有選項添加到它的頁面加載通過jQuery的ID。我設法用jQuery實現這一點,然後用$.post發送數據。問題出在我發送信息的文件中:

public ActionResult Export() 
     { 
      ... 

      var csvData = _utility.GetCsvData(data); 

      return File(Encoding.UTF8.GetBytes(csvData), "text.csv", "invoices.csv"); 
     } 

調出保存/打開文件對話框。我被告知這不會爲jQuery Ajax調用工作,我需要使用提交發布信息。

這很好,但現在我不知道如何將選擇ID和選中複選框的ID列表發送到方法。任何人都可以告訴我該怎麼做?

回答

3

你不需要任何HTML5數據 - *屬性,因爲當你提交表單不會被髮送到服務器。爲了發送他們的值,你將不得不使用AJAX,但這不適用於文件下載。所以,簡單地給你的複選框一個名字:

<td class="centered"> 
    <input type="checkbox" class="exportcheckbox" name="ids" value="@item.InvoiceId" /> 
</td> 

,然後在服務器上默認的模型綁定會自動構造的檢查項目的ID數組:

[HttpPost] 
public ActionResult Export(int[] ids) 
{ 
    byte[] data = ... 
    return File(data, "text/csv", "invoices.csv"); 
} 

取決於InvoiceId類型您可能需要調整動作參數的類型。

0

從根本上改變我的答案...

你可以隱藏的iframe動態地添加到您的網頁。 IFRAME src可以將您選擇的「ids」作爲查詢字符串參數。這應該讓你的下載對話框。

得到一些幫助的jQuery從這裏:JQuery: Turn array input values into a string optimization

var selectedIdsArray = $(":checked").map(function(){return $(this).attr('data-invoiceid');}); 
var url = '@Url.Action("Export", "Export")?csv=' selectedIdsArray.get().join(','); 
$('body').append("<iframe style='visibility:hidden' src='"+url +"'/>"); 
+0

從我已經被告知和體驗對話框將不會出現這樣的javascript帖子。必須提交表單或彈出的對話框不會出現。 – AnonyMouse

+0

更新了使用隱藏的iframe的答案,因此您可以獲得下載對話框。這意味着沒有阿賈克斯壽。 – russau

相關問題