public void MimicRealWorld() { var hbsview = new HandlebarsView(_controllerContext, _fixture.ViewUsingViewdata, layouts: null); ViewDataDictionary vdd = new ViewDataDictionary(); vdd["title"] = "Greetings"; vdd.Model = new Person() { FirstName = "John", LastName = "Doe" }; // The lines above are equivalent to (in a controller): // ViewBag.title = "Greetings"; // var model = new Person // { // FirstName = "John", // LastName = "Doe" // }; // return View(model); string html; using (var textWriter = new StringWriter()) { ViewContext viewContext = new ViewContext(_controllerContext, hbsview, vdd, tempData: new TempDataDictionary(), writer: textWriter); hbsview.Render(viewContext, textWriter); html = textWriter.GetStringBuilder().ToString(); } Assert.Equal("Title: Greetings. Hello, John Doe!", html); }
private string GetHtmlFromView(HandlebarsView hbsview) { string html = null; using (var textWriter = new StringWriter()) { ViewContext viewContext = new ViewContext(_controllerContext, hbsview, new ViewDataDictionary(_model), tempData: new TempDataDictionary(), writer: textWriter); hbsview.Render(viewContext, textWriter); html = textWriter.GetStringBuilder().ToString(); } return(html); }