Archive for June 2012
Using HTML content of a view rendered inside controller using Iview.Render()
HI this is an extension to my previous post Rendering a View differently without using return View() directly
In that post we saw how to render the html content of a view inside the controller using Iview.Render().
In this post i want to explain my business justification that made me to use this approach.
Why do i need html content of a view inside controller??
MyModel.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace MvcApplication2.Models { public class MyModel { public string firstname{set;get;} public string lastname{set;get;} public string AddressCity { set; get; } public string FullName { set; get; } public string nicknameFirstNmae { set; get; } public string profession { set; get; } public string designation { set; get; } public string experience { set; get; } } }
I am having a view called myFirstView.cshtml. This view consists of a grid that displays MyModel class data.
myFirstView.cshtml
@{ //Layout = null; } @model IEnumerable<MvcApplication2.Models.MyModel> <div> @{ List<string> ls=new List<string>(); var grid = new WebGrid(@Model); } @grid.GetHtml( tableStyle: "webgrid", headerStyle: "webgrid-header", alternatingRowStyle: "webgrid-alternating-row", selectedRowStyle: "webgrid-selected-row", rowStyle: "webgrid-row-style" ) </div>
just a preview of my rendered view
I am having a controller called FirstController. Inside this controller i am having a function called SendAndPreviewMail().
Here i am creating an object of MyModel class and then i am assigning values to properties.
I am having a class called renderView and it contains a static method called returnViewString(). That accepts object of controller,view name and model object.
In the function SendAndPreviewMail() i am creating model and then trying to render the view myFirstView.cshtml by passing controller,viewname,model to returnViewhtml().
FirstController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.IO; using System.Web.Mail; namespace MvcApplication2.Controllers { public class FirstController : Controller { public string SendAndPreviewMail() { Models.MyModel mod = new Models.MyModel(); Models.MyModel mod1 = new Models.MyModel(); List<Models.MyModel> lst = new List<Models.MyModel>(); mod.AddressCity = "Vizag"; mod.firstname = "Pavan"; mod.lastname = "Kumar"; mod.nicknameFirstNmae = "PavanArya"; mod.FullName = "Pavan Kumar Aryasomayajulu"; mod.experience = "2yrs"; mod.designation="Associate s/w engineer"; mod.profession="Information Technology"; mod1.AddressCity = "Vizag"; mod1.firstname = "Kiran"; mod1.lastname = "Kumar"; mod1.nicknameFirstNmae = "KiranMantri"; mod1.FullName = "Kiran Kumar Mantri"; mod1.experience = "3yrs"; mod1.designation = "s/w engineer"; mod1.profession = "Information Technology"; lst.Add(mod); lst.Add(mod1); string result = ReturnView.returnViewString(this, "myFirstView", lst);//we get view's html content try { MailMessage mail = new MailMessage(); System.Web.Mail.SmtpMail.SmtpServer = "localhost"; mail.From = "pavan.aryasomayajulu@gmail.com"; mail.To = "pavan.aryasomayajulu@gmail.com"; mail.Subject = "Test Mail"; mail.BodyFormat = MailFormat.Html; mail.BodyEncoding = System.Text.Encoding.Default; mail.Body = result; SmtpMail.Send(mail); } catch (Exception ex) { } return result; } } public class ReturnView { public static string returnViewString(Controller controller, String viewName, object model) { StringWriter sw = new StringWriter(); controller.ViewData.Model = model; ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName); ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw); viewResult.View.Render(viewContext, sw); string result = sw.ToString(); return result; } } }
After getting the view’s html content we are trying to send that data(rendered html content) as a mail using SMTP server.
MailMessage mail = new MailMessage(); System.Web.Mail.SmtpMail.SmtpServer = "localhost"; mail.From = "pavan.aryasomayajulu@gmail.com"; mail.To = "pavan.aryasomayajulu@gmail.com"; mail.Subject = "Test Mail"; mail.BodyFormat = MailFormat.Html; mail.BodyEncoding = System.Text.Encoding.Default; mail.Body = result; SmtpMail.Send(mail);
And at the same time we are outputting the views html content to browser.
So when we enter the url “http://localhost:1427/first/SendAndPreviewMail” our code is going to send html code of the view as a mail and at the same time it outputs the rendered view onto the screen as a preview.
Thanks,
Pavan