I want to export my model to excel
Model
public class CaseModel
{
public int CaseId { get; set; }
public long TenantId { get; set; }
public string CaseDescription { get; set; }
public List<CustomFields> CustomFields { get; set; }
public List<CustomFieldForUsers> CustomFieldForUser { get; set; }
}
I am getting predefined fields in excel but i am not getting list
objects i.e CustomFields
and CustomFieldForUser
Controller
public void ExportAsExcel()
{
List<CaseModel> lst = new List<CaseModel>();
if (Request.QueryString["caseids"] != null)
{
string[] collection = Request.QueryString["caseids"].Split(',');
foreach (var item in collection)
{
var model = caseservice.getCase(Tenant.Id, int.Parse(item));
model.CustomFieldForUser = caseservice.getCustomFieldsForUser(Tenant.Id, int.Parse(item));
model.CustomFields = caseservice.getCustomFields(Tenant.Id);
lst.Add(model);
}
}
var result = lst.Select(x => new
{
x.RefId,
x.CaseDescription,
x.CreatedBy,
CreatedDate = String.Format("{0:MMM d, yyyy}", x.CreatedDate),
CaseStatus = Enum.GetName(typeof(CaseStatus), x.CaseStatus),
CasePriority = Enum.GetName(typeof(CasePriority), x.CasePriority),
AssignedTo = x.AssignedTo == null || x.AssignedTo == "0" ? "NA" : tenantservice.getTenantUserById(Tenant.Id, x.AssignedTo).RealName,
Company = x.CompanyId == 0 ? "NA" : companydetailservice.getCompanyDetails(Tenant.Id, x.CompanyId).Name,
**//How to get the List objects here to be downloaded in excel since they are dynamic based on user who has loggedin**
});
GridView gv = new GridView();
gv.DataSource = result;
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + Tenant.SubDomain + "_Cases_" + DateTime.Now.ToShortDateString() + ".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();
}