vnext
Paul Schneider 8 years ago
parent 0582421523
commit b627544e4f
2 changed files with 22 additions and 5 deletions

@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity; using Microsoft.Data.Entity;
using Yavsc.Models; using Yavsc.Models;
@ -16,8 +17,14 @@ namespace Yavsc.ViewComponents
{ {
this.dbContext = dbContext; this.dbContext = dbContext;
} }
public IViewComponentResult InvokeAsync(long id, bool toPdf = false) public async Task<IViewComponentResult> InvokeAsync(long id)
{ {
return await InvokeAsync(id,"Html");
}
public async Task<IViewComponentResult> InvokeAsync(long id, string outputFormat ="Html")
{
return await Task.Run( ()=> {
Estimate estimate = Estimate estimate =
dbContext.Estimates.Include(x => x.Query) dbContext.Estimates.Include(x => x.Query)
.Include(x => x.Query.Client) .Include(x => x.Query.Client)
@ -27,11 +34,15 @@ namespace Yavsc.ViewComponents
.Include(e => e.Bill).FirstOrDefault(x => x.Id == id); .Include(e => e.Bill).FirstOrDefault(x => x.Id == id);
if (estimate == null) if (estimate == null)
throw new Exception("No data"); throw new Exception("No data");
if (toPdf)
if (outputFormat == "LaTeX") {
return this.View("Estimate_tex", estimate);
}
else if (outputFormat == "Pdf")
{ {
// Sorry for this code
string tex = null; string tex = null;
var oldWriter = ViewComponentContext.ViewContext.Writer; var oldWriter = ViewComponentContext.ViewContext.Writer;
using (var writer = new StringWriter()) using (var writer = new StringWriter())
{ {
this.ViewComponentContext.ViewContext.Writer = writer; this.ViewComponentContext.ViewContext.Writer = writer;
@ -50,7 +61,9 @@ namespace Yavsc.ViewComponents
BaseFileName = $"estimate-{id}" BaseFileName = $"estimate-{id}"
} ); } );
} }
return this.View("Estimate_tex", estimate); return View("Default",estimate);
}
);
} }
} }

@ -1,14 +1,18 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Microsoft.AspNet.Mvc.Rendering;
namespace Yavsc.ViewModels.Gen namespace Yavsc.ViewModels.Gen
{ {
public class PdfGenerationViewModel public class PdfGenerationViewModel
{ {
[Required] [Required]
public string TeXSource { get; set; } public string TeXSource { get; set; }
[Required] [Required]
public string BaseFileName { get; set; } public string BaseFileName { get; set; }
[Required] [Required]
public string DestDir { get; set; } public string DestDir { get; set; }
public bool Generated { get; set; }
public HtmlString GenerationErrorMessage { get; set; }
} }
} }
Loading…