如何将完整模型从视图传递到控制器mvc4

时间:2013-12-02 13:27:58

标签: jquery ajax asp.net-mvc-4

    i need to export data from a model class to excel.i have tried many  ways but i couldn't pass the data from my view to controller.i have successfully rendered a partial view which displays data in a tabular format.i have used foreach to iterate through model.
 i have one partial`enter code here` view which displays data from model.                
               @model HRMS.Models.EligibilityCriteriaModel
            @{
                ViewBag.Title = "Successfully Initiated Employees";
            }
            <div>
                <h2 class="title-bgcolor">
                    Successfully Initiated Employees</h2>
                <br />
                <div id="AllEmployeeDiv">
                    <div class="stackhold-right">
                        <input class="button" type="button" id="printSuccessEmp" value="Print" onclick="printDiv('printThisArea')" />
                        <input class="button" type="button" id="exportSuccessExcel" value="Export to Excel" />
                        <input class="button" type="button" id="okSuccessEmp" value="OK" />
                    </div>
                    <br />
                    <br />
                    <div id="printThisArea">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%" id="tbl_allSuccessEmployees" class="TablesBlueHeaders">
                            <thead>
                                <tr class="bluebgtable1">
                                    <td width="10%">
                                        Employee Code
                                    </td>
                                    <td class="bluebgtable1" width="15%">
                                        Employee Name
                                    </td>
                                    <td class="bluebgtable1" width="15%">
                                        Delivery Team
                                    </td>
                                    <td class="bluebgtable1" width="15%">
                                        Designation
                                    </td>
                                    <td class="bluebgtable1" width="15%">
                                        Confirmation Date
                                    </td>
                                </tr>
                            </thead>
                            <div>
                                @foreach (var item in Model.allSuccessEmployeeList)//this is the model which i need to export to excel i have values in model.
                                {
                                    <tr id="@item.EmployeeID" class="highlightRed">
                                        <td align="center">
                                            @Html.DisplayFor(modelItem => item.EmployeeCode, new { @readonly = "readonly" })
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.EmployeeName, new { @readonly = "readonly" })
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.DeliveryTeam, new { @readonly = "readonly" })
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.Designation, new { @readonly = "readonly" })
                                        </td>
                                        <td>
                                            @Html.DisplayFor(modelItem => item.ConfirmationDate, new { @readonly = "readonly" })
                                        </td>
                                    </tr>
                                }
                            </div>
                        </table>
                    </div>
                </div>
            </div>


     <script type="text/javascript">
                    function printDiv(printThisArea) {
                        $("#allEmpSuccessDialog").dialog("close");
                        var printContents = document.getElementById(printThisArea).innerHTML;
                        var originalContents = document.body.innerHTML;
                        document.body.innerHTML = printContents;
                        window.print();
                        document.body.innerHTML = originalContents;
                        window.location.href = '@Url.Action("EligibilityCriteria", "ConfigurationAppraisal")';
                    }
//this is the ajax call on which on click of the button data will pass to conroller and controller method will generate excel report.                
                    $('#exportSuccessExcel').on('click', function () {
                        var postUrl = '@Url.Action("ExportData", "ConfigurationAppraisal")';
                        $.ajax({
                            url: postUrl,
                            type: 'POST',
                            data: { completeData: '@Model.allSuccessEmployeeList' },
                            success: function (results) {
                            },
                            error: function (results) {
                            }
                        });
                    });
                </script>


            now i want to pass complete model (Model.allSuccessEmployeeList) to controller which will generate the exported excel.
        Controller code is:
        public ActionResult ExportData(List<AllEligibileEmployee> completeData)
                {//code to generate excel report
                    GridView gv = new GridView();
                    gv.DataSource = completeData;
                    gv.DataBind();
                    Response.ClearContent();
                    Response.Buffer = true;
                    Response.AddHeader("content-disposition", "attachment; filename=Download.xls");
                    Response.ContentType = "application/ms-excel";
                    Response.Charset = "";
                    StringWriter sw = new StringWriter();
                    HtmlTextWriter htw = new HtmlTextWriter(sw);
                    gv.RenderControl(htw);
                    Response.Output.Write(sw.ToString());
                    Response.Flush();
                    Response.End();
                    return RedirectToAction("StudentDetails");
                }
    My controller doesnt get completeData it shows count as 0.i want to know how can i send complete model to the controller which will export the data to excel.
    i have tried json.stringify but it didnt work.kindly help if any one of the experts have done this issue.

顺便说一下,我总是收到错误,请添加一些上下文来解释代码部分或检查您是否没有错误地将所有问题格式化为代码。

1 个答案:

答案 0 :(得分:1)

您可以使用$('form').serialize()

传递整个模型

使用

将整个标记放入表单中
 @using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { id = "yourForm" }))
{

// html标记就在这里 }

然后你可以使用表格的id或形式本身来获得完整的模型

你可以使用

$('form').serialize()

$('#yourForm').serialize()

ajax调用的代码是。

$.ajax({
                            url: postUrl,
                            type: 'POST',
                            data: { completeData: $('form').serialize()  },
                            success: function (results) {
                            },
                            error: function (results) {
                            }
                        });

然后在控制器操作中,您可以接受整个模型作为参数

public ActionResult ExportData(EligibilityCriteriaModel completeData)