Note that there are some explanatory texts on larger screens.

plurals
  1. POexport model data to excel mvc
    text
    copied!<pre><code>@model SA.MarketingManager.WebClient.Areas.Reports.Models.ViewReportModel @{ Layout = "~/CustomViews/lennoxcap.net/Shared/_DealerLayout.cshtml"; } @using (Html.BeginForm()) { &lt;div style="width: 100%; font-size: 9px;" id="results"&gt; &lt;h3 style="background-color: #CC0000; color: #fff; font-size: 1.5em;"&gt; Customer Survey Report&lt;/h3&gt; @if (Model.Report.Count() &gt; 0) { &lt;table id="SurveyResponse" width="100%" cellpadding="0" cellspacing="0"&gt; &lt;thead&gt; &lt;tr class="header"&gt; &lt;td&gt; Dealer # &lt;/td&gt; &lt;td&gt; District &lt;/td&gt; &lt;td&gt; TM &lt;/td&gt; &lt;td&gt; Survey Code &lt;/td&gt; &lt;td&gt; First Name &lt;/td&gt; &lt;td&gt; Last Name &lt;/td&gt; &lt;td&gt; Address &lt;/td&gt; &lt;td&gt; City &lt;/td&gt; &lt;td&gt; State &lt;/td&gt; &lt;td&gt; Postal Code &lt;/td&gt; &lt;td&gt; Phone &lt;/td&gt; &lt;td&gt; Mail Sent &lt;/td&gt; &lt;td&gt; Email Sent &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; @{bool alternate = false;} @foreach (var tr in Model.Report) { &lt;tr @(alternate ? "class=alternate" : "")&gt; &lt;td&gt; @tr.DealerId &lt;/td&gt; &lt;td&gt; @tr.District &lt;/td&gt; &lt;td&gt;@tr.TM &lt;/td&gt; &lt;td&gt;@tr.SurveyCode &lt;/td&gt; &lt;td&gt;@tr.FirstName &lt;/td&gt; &lt;td&gt;@tr.LastName &lt;/td&gt; &lt;td&gt;@tr.Address &lt;/td&gt; &lt;td&gt;@tr.City &lt;/td&gt; &lt;td&gt;@tr.State &lt;/td&gt; &lt;td&gt;@tr.PostalCode &lt;/td&gt; &lt;td&gt;@tr.Phone &lt;/td&gt; &lt;td&gt;@tr.MailSent &lt;/td&gt; &lt;td&gt;@tr.DateCompleted &lt;/td&gt; &lt;/tr&gt; alternate = !alternate; } &lt;/tbody&gt; &lt;/table&gt; } else { &lt;text&gt;There are no records to display&lt;/text&gt; } &lt;p&gt; &lt;input type="submit" id="Submit" value="Export Data" class="PremierSubmitButton" /&gt; &lt;/p&gt; &lt;/div&gt; </code></pre> <p>controller</p> <pre><code>public ActionResult CustomerReport() { ViewReportModel model = new ViewReportModel(); var query = (from u in SessionHandler.CurrentContext.LennoxSurveyResponses join c in SessionHandler.CurrentContext.MailingListEntries on u.SurveyCode equals c.SurveyCode join cl in SessionHandler.CurrentContext.MailingLists on c.MailingListId equals cl.MailingListId join ch in SessionHandler.CurrentContext.Channels on cl.ChannelId equals ch.ChannelId join cg in SessionHandler.CurrentContext.ChannelGroups on ch.ChannelGroupId equals cg.ChannelGroupId //let con = ch.Contacts.FirstOrDefault() where ch.OrganizationId == 8 //&amp;&amp; con.ContactTypeId == ContactTypeConstants.TMId //select new ReportDetails() { SurveyResponse = u, MailingListEntry = c, Channel = cl.Channel, Contact = con }); select new ReportDetails{ DealerId = ch.ExternalChannelId, District = ch.ChannelAMSData.District, TM = cg.Name, SurveyCode = u.SurveyCode, FirstName = c.FirstName, LastName = c.LastName, Address = c.Address1, City = c.City, State = c.State, PostalCode = c.PostalCode, Email = c.Email, Phone = c.Phone, MailSent = c.LetterDate, DateCompleted = c.EmailDate}); model.Report = query; return View("CustomerReport", model); } [HttpPost] public ActionResult CustomerReport(ViewReportModel model) { var query = (from u in SessionHandler.CurrentContext.LennoxSurveyResponses join c in SessionHandler.CurrentContext.MailingListEntries on u.SurveyCode equals c.SurveyCode join cl in SessionHandler.CurrentContext.MailingLists on c.MailingListId equals cl.MailingListId join ch in SessionHandler.CurrentContext.Channels on cl.ChannelId equals ch.ChannelId join cg in SessionHandler.CurrentContext.ChannelGroups on ch.ChannelGroupId equals cg.ChannelGroupId where ch.OrganizationId == 8 select new ReportDetails { DealerId = ch.ExternalChannelId, District = ch.ChannelAMSData.District, TM = cg.Name, SurveyCode = u.SurveyCode, FirstName = c.FirstName, LastName = c.LastName, Address = c.Address1, City = c.City, State = c.State, PostalCode = c.PostalCode, Email = c.Email, Phone = c.Phone, MailSent = c.LetterDate, DateCompleted = c.EmailDate }); model.Report = query; return new Utilities.ExcelResult&lt;ViewReportModel&gt;( ControllerContext, "~/ExcelReport.aspx", "CustomerReport.xls", model ); } </code></pre> <p>My model ReportsModel.cs</p> <pre><code>public class ReportDetails { public string DealerId { get; set; } public string District { get; set; } public string TM { get; set; } public string SurveyCode { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } public string State { get; set; } public string PostalCode { get; set; } public string Email { get; set; } public string Phone { get; set; } public DateTime? MailSent { get; set; } public DateTime? DateCompleted { get; set; } } public class ViewReportModel : PageModel { public IEnumerable&lt;ReportDetails&gt; Report { get; set; } public string Results { get; set; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload