1
回答
MVC FileResult处理问题,求解决!!!
终于搞明白,存储TCO原来是这样算的>>>   

项目使用MVC 3.0,遇到一个报表导出的问题。

想在点击报表导出按钮时,禁用按钮,然后出现进度图片,等待Control中方法执行完成后

再启用按钮。功能已经做出来了,但是,经过这样控制之后,Control中的FileResult不能变成Excel文件下载了。

代码如下:

1.View中原代码,可以导出Excel:


@model Myj.ELife.Financial.WebUI.Models.ReportModel
@{
    ViewBag.Title = "Index";
}

<div id="content">
    <fieldset>
        <legend>报表导出</legend>
        <div class="edit">
        @using (Html.BeginForm("Index", "Report", FormMethod.Post))
        {
            <table cellpadding="0" cellspacing="0" class="table_edit">
             <tr>
                <th>@Html.Encode("报表类型:")</th>
                <td>
                    @Html.DropDownList("ReporType", Model.AvailableReportTypeStatuses)
                </td>
                <th>@Html.Encode("业务类型:")</th>
               <td>
                @Html.DropDownList("BusinessType", Model.AvailableBusinessTypeStatuses)
               </td>
               </tr>
                <tr>
                    <th>
                        @Html.Encode("开始时间:")
                    </th>
                    <td> 
                        @Html.TextBox("BeginTime", null, new { @class = "mediumfield", @id = "txtBeginTime", onfocus = "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',readOnly:true})" })
                    </td>
                
                    <th>
                        @Html.Encode("截止时间:")
                    </th>
                    <td> 
                        @Html.TextBox("EndTime", null, new { @class = "mediumfield", @id = "txtEndTime", onfocus = "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',readOnly:true})" })
                    </td>
                </tr>
                 <tr>
                        <td colspan="4" style="text-align: center;">
                            <input class="button_big" type="submit" id="i_Export" value="报表导出" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" style="text-align: center;">
                            <div id="message" style="color: Red">
                            </div>
                        </td>
                    </tr>
            </table>     
        }
        </div>
    </fieldset>
</div>
@*<script src="@Url.Content("../../Scripts/My97DatePicker/WdatePicker.js")" type="text/javascript"></script>*@

<script type="text/javascript">
    $(document).ready(function () {
        $('select[id=ReporType]').change(function () {

            var id = $('select[id=ReporType]').val();
            //门店余额查询及深圳通明细查询(结算)不需要供应商
            if (id == 2 || id == 5) {
                $("#BusinessType").empty();
                $("<option></option>").val("").html("--请选择--").appendTo("#BusinessType");
                $('select[id=BusinessType]').attr("disabled", true);
                return false;
            }
            $('select[id=BusinessType]').attr("disabled", false);
            var url = '@Url.Content("~/Report/GetAccountDetails?id=")' + id + "&" + Math.random();
            $.getJSON(url, function (result) {
                $("#BusinessType").empty();
                $("<option></option>").val("").html("--请选择--").appendTo("#BusinessType");
                $.each(result, function (i) {
                    $("#BusinessType").append($("<option></option>").val(result[i].SupplierCode).html(result[i].SupplierName))
                });
            });
        });

        $('#i_Export').click(function () {
            var reporTypeId = $('select[id=ReporType]').val();
            if (typeof (reporTypeId) == undefined || reporTypeId == '') {
                alert("请选择报表类型");
                return false;
            }

            // 检查选择的业务类型,排除门店余额查询及深圳通明细查询(结算)
            if (reporTypeId != 2 && reporTypeId != 5) {
                var bussinessId = $('select[id=BusinessType]').val();
                if (typeof (bussinessId) == undefined || bussinessId == '') {
                    alert("请选择业务类型");
                    return false;
                }
            }

            //检查输入的日期是否合法
            var beginTime = $('#txtBeginTime').val();
            var endTime = $('#txtEndTime').val();

            if (typeof (beginTime) == undefined || beginTime == '') {
                alert("请输入开始时间!");
                return false;
            }

            if (typeof (endTime) == undefined || endTime == '') {
                alert("请输入结束时间!");
                return false;
            }

            var beginDate = new Date(beginTime.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1/$2/$3"));
            var endDate = new Date(endTime.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1/$2/$3"));

            if (beginDate > endDate) {
                alert("开始时间不能大于结束时间!");
                return false;
            }
        });
    })
</script>
2.View修改后代码,可以实现点击报表导出按钮禁用,并出现转动图标,后台执行完成后,按钮启用。


但是此时浏览器好像能接收到服务器post回来的数据,但是不处理成EXCEL下载了。

BeginForm中的UpdateTargetId="result",是我调试用的,浏览器会把所有数据直接写到result控件上,如果不给这个UpdateTargetId就会没反映。


@model Myj.ELife.Financial.WebUI.Models.ReportModel
@{
    ViewBag.Title = "Index";
}

<div id="content">
    <fieldset>
        <legend>报表导出</legend>
        <div class="edit">
        @using (Ajax.BeginForm("Index", "Report", new AjaxOptions {
            LoadingElementId = "message",
       OnBegin = "startFunc",
       OnSuccess = "completeFunc" 
       ,UpdateTargetId="Result"
        }))
        {
            <table cellpadding="0" cellspacing="0" class="table_edit">
             <tr>
                <th>@Html.Encode("报表类型:")</th>
                <td>
                    @Html.DropDownList("ReporType", Model.AvailableReportTypeStatuses)
                </td>
                <th>@Html.Encode("业务类型:")</th>
               <td>
                @Html.DropDownList("BusinessType", Model.AvailableBusinessTypeStatuses)
               </td>
               </tr>
                <tr>
                    <th>
                        @Html.Encode("开始时间:")
                    </th>
                    <td> 
                        @Html.TextBox("BeginTime", null, new { @class = "mediumfield", @id = "txtBeginTime", onfocus = "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',readOnly:true})" })
                    </td>
                
                    <th>
                        @Html.Encode("截止时间:")
                    </th>
                    <td> 
                        @Html.TextBox("EndTime", null, new { @class = "mediumfield", @id = "txtEndTime", onfocus = "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',readOnly:true})" })
                    </td>
                </tr>
                 <tr>
                        <td colspan="4" style="text-align: center;">
                            <input class="button_big" type="submit" id="i_Export" value="报表导出" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" style="text-align: center;">
                            <div id="message" style="color: Red;display:none;">
                                 <img id="loadingImg" src="../../Content/themes/images/loadingData.gif" />正在导出...
                            </div>
                        </td>
                    </tr>
                     <tr>
                        <td colspan="4" style="text-align: center;">
                            <div id="Result" style="color: Red;">
                            </div>
                        </td>
                    </tr>
            </table>     
  
        }
        </div>
    </fieldset>
</div>
@*<script src="@Url.Content("../../Scripts/My97DatePicker/WdatePicker.js")" type="text/javascript"></script>*@
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

<script type="text/javascript">
    var startFunc = function () {
        $('input[id=i_Export]').attr('disabled', true);
        $('input[id=i_Export]').removeAttr('class');
    }
    var completeFunc = function () {
        $('input[id=i_Export]').removeAttr('disabled');
        $('input[id=i_Export]').attr('class', 'button_big');
    }
    $(document).ready(function () {

        $('select[id=ReporType]').change(function () {

            var id = $('select[id=ReporType]').val();
            //门店余额查询及深圳通明细查询(结算)不需要供应商
            if (id == 2 || id == 5) {
                $("#BusinessType").empty();
                $("<option></option>").val("").html("--请选择--").appendTo("#BusinessType");
                $('select[id=BusinessType]').attr("disabled", true);
                return false;
            }
            $('select[id=BusinessType]').attr("disabled", false);
            var url = '@Url.Content("~/Report/GetAccountDetails?id=")' + id + "&" + Math.random();
            $.getJSON(url, function (result) {
                $("#BusinessType").empty();
                $("<option></option>").val("").html("--请选择--").appendTo("#BusinessType");
                $.each(result, function (i) {
                    $("#BusinessType").append($("<option></option>").val(result[i].SupplierCode).html(result[i].SupplierName))
                });
            });
        });
      
        $('#i_Export').click(function () {
            var reporTypeId = $('select[id=ReporType]').val();
            if (typeof (reporTypeId) == undefined || reporTypeId == '') {
                alert("请选择报表类型");
                return false;
            }

            // 检查选择的业务类型,排除门店余额查询及深圳通明细查询(结算)
            if (reporTypeId != 2 && reporTypeId != 5) {
                var bussinessId = $('select[id=BusinessType]').val();
                if (typeof (bussinessId) == undefined || bussinessId == '') {
                    alert("请选择业务类型");
                    return false;
                }
            }

            //检查输入的日期是否合法
            var beginTime = $('#txtBeginTime').val();
            var endTime = $('#txtEndTime').val();

            if (typeof (beginTime) == undefined || beginTime == '') {
                alert("请输入开始时间!");
                return false;
            }

            if (typeof (endTime) == undefined || endTime == '') {
                alert("请输入结束时间!");
                return false;
            }

            var beginDate = new Date(beginTime.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1/$2/$3"));
            var endDate = new Date(endTime.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1/$2/$3"));

            if (beginDate > endDate) {
                alert("开始时间不能大于结束时间!");
                return false;
            }
        });
    })
</script>
2.Control中导出代码



[HttpPost]
        public FileResult Index(ReportModel model)
        {
            if (!model.ReporType.HasValue)
            {
                return null;
            }

            byte reportTypeValue = 0;
            string reportName = string.Empty;
            ReportType accountMaster = _reportService.GetReportType(model.ReporType.Value);
            if (accountMaster == null)
            {
                return null;
            }
            else
            {
                reportTypeValue = accountMaster.Id;
                reportName = accountMaster.Name;
            }

            byte businessTypeValue = 0;
            string businessName = string.Empty;
            if (model.BusinessType.HasValue)
            {
                businessTypeValue = model.BusinessType.Value;
                businessName = _reportService.GetReportBusinessName(reportTypeValue, businessTypeValue);
            }

            var getUser = GetUser();
            ReportSearchCondition rsc = new ReportSearchCondition
            {
                ReportName = reportName,
                BusinessID = businessTypeValue,
                BusinessName = businessName,
                BeginTime = model.BeginTime.Value,
                EndTime = model.EndTime.Value.AddMilliseconds(997),
                OperaterID = getUser.UserCode,
                OperaterName = getUser.UserName
            };

            Func<ReportSearchCondition, DataSet> func = GetReportMethod(reportTypeValue);

            string fileName = string.Format("{0}从{1}到{2}",
                                                  rsc.BusinessID == 0 ? rsc.ReportName : rsc.BusinessName,
                                                  Convert.ToDateTime(rsc.BeginTime).ToString("yyMMdd"),
                                                  Convert.ToDateTime(rsc.EndTime).ToString("yyMMdd"));
            ExportDataToExcel(func, rsc);
            return File(fileName, "application/vnd.ms-excel");
        }


举报
顶部