Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 2 View (ViewModel) -> MS Word or PDF Generation?
    text
    copied!<p>Id like to have my mvc 2 app generating reports in both MS Word and PDF formats....currently working on Word. I found this: </p> <p><a href="http://www.revium.com.au/articles/sandbox/aspnet-mvc-convert-view-to-word-document/" rel="nofollow noreferrer">http://www.revium.com.au/articles/sandbox/aspnet-mvc-convert-view-to-word-document/</a></p> <p>Which i think basically streams the view output from a controller action to a word document....</p> <pre><code>public ActionResult DetailedReport() { //[...] return View(); } public ActionResult DetailedReportWord() { string url = "DetailedReport"; return new WordActionResult(url, "detailed-report.doc"); } </code></pre> <p>And heres the class that extends ActionResult</p> <pre><code>public class WordActionResult : ActionResult { private string Url { get; set;} private string FileName { get; set;} public WordActionResult(string url, string fileName) { Url = url; FileName = fileName; } public override void ExecuteResult(ControllerContext context) { var curContext = HttpContext.Current; curContext.Response.Clear(); curContext.Response.AddHeader("content-disposition", "attachment;filename=" + FileName); curContext.Response.Charset = ""; curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); curContext.Response.ContentType = "application/ms-word"; var wreq = (HttpWebRequest) WebRequest.Create(Url); var wres = (HttpWebResponse) wreq.GetResponse(); var s = wres.GetResponseStream(); var sr = new StreamReader(s, Encoding.ASCII); curContext.Response.Write(sr.ReadToEnd()); curContext.Response.End(); } </code></pre> <p>} </p> <p>Which looks pretty good - but my problem is that my view renders from a ViewModel, here is the Action</p> <pre><code>[HttpPost] public ActionResult StartSearch(SearchResultsViewModel model) { SearchResultsViewModel resultsViewModel = _searchService.Search(model.Search, 1, PAGE_SIZE); return View("Index", resultsViewModel); } </code></pre> <p>and here is my model:</p> <pre><code>public class SearchResultsViewModel { [SearchWordLimit] public string Search { get; set; } public IEnumerable&lt;Employee&gt; Employees { get; private set; } public IEnumerable&lt;Organization&gt; Organizations { get; private set; } public PageInfo PageInfo { get; private set;} public SearchResultsViewModel() { } public SearchResultsViewModel(string searchString, IEnumerable&lt;Employee&gt; employees, IEnumerable&lt;Organization&gt; organizations, PageInfo pageInfo) { Search = searchString; Employees = employees; Organizations = organizations; PageInfo = pageInfo; } } </code></pre> <p>I'm having trouble kinda connecting the two - to stream the view using my viewmodel to pdf</p>
 

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