Jquery 如何将完整模型从视图传递到控制器mvc4 我需要将数据从模型类导出到excel。我尝试了许多方法,但无法将数据从视图传递到控制器。我已成功呈现了以表格格式显示数据的局部视图。我已使用foreach在模型中进行迭代。 我有一个局部的“在这里输入代码”视图,它显示来自模型的数据。 @模型HRMS.Models.EligibilityCriteriaModel @{ ViewBag.Title=“成功启动员工”; } 成功启动的员工 员工代码 员工姓名 交付团队 任命 确认日期 @foreach(Model.allSuccessEmployeeList中的var项)//这是我需要导出到excel的模型,我在模型中有值。 { @DisplayFor(modelItem=>item.EmployeeCode,新的{@readonly=“readonly”}) @DisplayFor(modelItem=>item.EmployeeName,新的{@readonly=“readonly”}) @DisplayFor(modelItem=>item.DeliveryTeam,新的{@readonly=“readonly”}) @DisplayFor(modeleItem=>item.Designation,新的{@readonly=“readonly”}) @DisplayFor(modelItem=>item.ConfirmationDate,新的{@readonly=“readonly”}) } 函数printDiv(printThisArea){ $(“allempsccessdialog”).dialog(“关闭”); var printContents=document.getElementById(printThisArea.innerHTML; var originalContents=document.body.innerHTML; document.body.innerHTML=打印内容; window.print(); document.body.innerHTML=原始内容; window.location.href='@Url.Action(“EligibilityCriteria”、“配置评估”); } //这是一个ajax调用,点击按钮数据将传递给conroller,控制器方法将生成excel报告。 $('#exportSuccessExcel')。在('click',函数(){ var postrl='@Url.Action(“ExportData”,“configurationevaluation”); $.ajax({ 网址:postrl, 键入:“POST”, 数据:{completeData:'@Model.allSuccessEmployeeList'}, 成功:功能(结果){ }, 错误:函数(结果){ } }); }); 现在我想将完整的模型(model.allSuccessEmployeeList)传递给控制器,控制器将生成导出的excel。 控制器代码为: 公共操作结果导出数据(列表完成数据) {//生成excel报表的代码 GridView gv=新的GridView(); gv.DataSource=completeData; gv.DataBind(); Response.ClearContent(); Response.Buffer=true; AddHeader(“内容处置”、“附件;文件名=Download.xls”); Response.ContentType=“应用程序/ms excel”; 响应。Charset=“”; StringWriter sw=新的StringWriter(); HtmlTextWriter htw=新的HtmlTextWriter(sw); gv.渲染控制(htw); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); 返回重定向到操作(“学生详细信息”); } 我的控制器未获得CompletedData,它显示计数为0。我想知道如何将完整模型发送到控制器,控制器将数据导出到excel。 我已经尝试过json.stringify,但它不起作用。如果有专家做过这个问题,请提供帮助。
顺便说一句,我总是出错,请添加一些上下文来解释代码部分,或者检查您是否有错误Jquery 如何将完整模型从视图传递到控制器mvc4 我需要将数据从模型类导出到excel。我尝试了许多方法,但无法将数据从视图传递到控制器。我已成功呈现了以表格格式显示数据的局部视图。我已使用foreach在模型中进行迭代。 我有一个局部的“在这里输入代码”视图,它显示来自模型的数据。 @模型HRMS.Models.EligibilityCriteriaModel @{ ViewBag.Title=“成功启动员工”; } 成功启动的员工 员工代码 员工姓名 交付团队 任命 确认日期 @foreach(Model.allSuccessEmployeeList中的var项)//这是我需要导出到excel的模型,我在模型中有值。 { @DisplayFor(modelItem=>item.EmployeeCode,新的{@readonly=“readonly”}) @DisplayFor(modelItem=>item.EmployeeName,新的{@readonly=“readonly”}) @DisplayFor(modelItem=>item.DeliveryTeam,新的{@readonly=“readonly”}) @DisplayFor(modeleItem=>item.Designation,新的{@readonly=“readonly”}) @DisplayFor(modelItem=>item.ConfirmationDate,新的{@readonly=“readonly”}) } 函数printDiv(printThisArea){ $(“allempsccessdialog”).dialog(“关闭”); var printContents=document.getElementById(printThisArea.innerHTML; var originalContents=document.body.innerHTML; document.body.innerHTML=打印内容; window.print(); document.body.innerHTML=原始内容; window.location.href='@Url.Action(“EligibilityCriteria”、“配置评估”); } //这是一个ajax调用,点击按钮数据将传递给conroller,控制器方法将生成excel报告。 $('#exportSuccessExcel')。在('click',函数(){ var postrl='@Url.Action(“ExportData”,“configurationevaluation”); $.ajax({ 网址:postrl, 键入:“POST”, 数据:{completeData:'@Model.allSuccessEmployeeList'}, 成功:功能(结果){ }, 错误:函数(结果){ } }); }); 现在我想将完整的模型(model.allSuccessEmployeeList)传递给控制器,控制器将生成导出的excel。 控制器代码为: 公共操作结果导出数据(列表完成数据) {//生成excel报表的代码 GridView gv=新的GridView(); gv.DataSource=completeData; gv.DataBind(); Response.ClearContent(); Response.Buffer=true; AddHeader(“内容处置”、“附件;文件名=Download.xls”); Response.ContentType=“应用程序/ms excel”; 响应。Charset=“”; StringWriter sw=新的StringWriter(); HtmlTextWriter htw=新的HtmlTextWriter(sw); gv.渲染控制(htw); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); 返回重定向到操作(“学生详细信息”); } 我的控制器未获得CompletedData,它显示计数为0。我想知道如何将完整模型发送到控制器,控制器将数据导出到excel。 我已经尝试过json.stringify,但它不起作用。如果有专家做过这个问题,请提供帮助。,jquery,ajax,asp.net-mvc-4,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
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.
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { id = "yourForm" }))
{
$('form').serialize()
$('#yourForm').serialize()
$.ajax({
url: postUrl,
type: 'POST',
data: { completeData: $('form').serialize() },
success: function (results) {
},
error: function (results) {
}
});
public ActionResult ExportData(EligibilityCriteriaModel completeData)