将整个数据表从视图传递到控制器操作方法

时间:2013-09-08 07:36:08

标签: asp.net-mvc razor

我有以下观点:

<table cellpadding="0" cellspacing="0" border="0" class="display mobile_dt1" id="SearchResults">
    <thead>
        <tr>
            @*   <input type="checkbox" name="Test" onclick="SetAllCheckBoxes('PartialViewName')" *@
            <th id="CHCKID" class="checkBoxClass">Check All @Html.CheckBox("checkboxall", new { @id = "checkall", @name = "StatusList" })</th>
            <th>Export</th>
            <th>Request ID</th>
            <th id="RQSTNMSRCHID" style="display: none">@BVPConstants.RQSTNMSRCH</th>
            <th id="RQSTSTTSSRCHID">@BVPConstants.RQSTSTTSSRCH</th>
            <th>Strategic Initiative</th>
            <th id="BSNGRPL1ID">@BVPConstants.BSNGRPL1</th>
            <th id="EXCTVSPNSRID">@BVPConstants.EXCTVSPNSR</th>
            <th id="RQSTCLSFNID">@BVPConstants.RQSTCLSFN</th>
            <th id="BUSNDBYDTID">@BVPConstants.BUSNDBYDT</th>
            <th id="CRTDBYSRCHID">@BVPConstants.CRTDBYSRCH</th>
            <th id="CRTDONID">@BVPConstants.CRTDON</th>
        </tr>
    </thead>
    <tbody>
        @if (Model != null)
        {
        foreach (var SearchResult in Model)
        {
        <tr>
            <td class="chck">@Html.CheckBox("checkboxall", new { @id = "check" })</td>
            <td>@Html.ActionImage("", "", new { @Id = "export" }, null, null, null, "~/Content/img/ico/filePDFUpload.png", "Download")</td>
            <td>@Html.ActionLink(SearchResult.RequestId.ToString(), "Edit", "BusinessRequest", new { requestId = SearchResult.RequestId }, new { @onerror = "closeLoading()", @onload = "closeLoading()", @onclick = "showLoading()" })</td>
            <td>@Html.ActionLink(SearchResult.RequestName.ToString(), "Edit", "BusinessRequest", new { requestId = SearchResult.RequestId }, new { @onerror = "closeLoading()", @onload = "closeLoading()", @onclick = "showLoading()" }) </td>
            <td>@SearchResult.Status</td>
            <td>@SearchResult.StrategicInitiativeNumber</td>
            <td>@SearchResult.BusinessGroup</td>
            <td>@SearchResult.ExecutiveSponsorNumber</td>
            <td>@SearchResult.RequestClassification</td>
            <td>@SearchResult.BusinessNeedByDate</td>
            <td>@SearchResult.CreatedBy</td>
            <td>@SearchResult.CRTTs</td>
        </tr>
        }
        }
    </tbody>
</table>

我想将SearchResults数据表发送到以下控制器方法:

public PartialViewResult ExportToExcel(DataTable table) {
    List<SearchRequestModel> srchlist = new List<SearchRequestModel>();
    StringWriter stringWriter = new StringWriter();
    var grid = new GridView();

    grid.DataSource = table;
    grid.DataBind();
    Response.ClearContent();
    Response.AddHeader("Content-Disposition", "attachment; filename=Prioritization.xls");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
    grid.RenderControl(htmlTextWriter);
    Response.Write(stringWriter.ToString());
    Response.End();

    return PartialView("_SearchHistory", srchlist);
}

1 个答案:

答案 0 :(得分:0)

Action Method参数自动映射Request.Form。也就是说,如果表格中有表单字段,您将只能使DataTable参数有效(有值)。

由于您不希望允许用户编辑数据,我建议您使用隐藏字段。您可以“手动”(<input type="hidden" />)或使用MVC Html帮助程序(@Html.HiddenFor())来执行它们。