public static string ExecuteTemplate(ControllerContext context, INotificationDefinition definition, NotificationState state) { string name = definition.GetType().Name; string fullViewName = "NotificationTemplate/" + name; Type modelType = typeof(NotificationModel <>); Type[] typeArgs = { definition.GetType() }; Type g = modelType.MakeGenericType(typeArgs); object o = Activator.CreateInstance(g); g.GetProperty("Definition").SetValue(o, definition, null); g.GetProperty("State").SetValue(o, state, null); context.Controller.ViewData.Model = o; string html = string.Empty; ViewEngineResult viewEngineResult = ViewEngines.Engines.FindPartialView(context, fullViewName); if (viewEngineResult.View != null) { using (var writer = new StringWriter(CultureInfo.InvariantCulture)) { viewEngineResult.View.Render( new ViewContext(context, viewEngineResult.View, context.Controller.ViewData, context.Controller.TempData, writer), writer); html = writer.ToString(); } } return(html); }
/// <summary> /// Send notification to all recipients. /// </summary> /// <param name="sender">Users object.</param> /// <param name="definition">INotificationDefinition object.</param> /// <param name="recipients">List of recipients for notification.</param> public void Notify(Users sender, INotificationDefinition definition, IList <Users> recipients) { if (sender == null) { throw new ArgumentNullException("sender"); } if (definition == null) { throw new ArgumentNullException("definition"); } var notification = new Notification { Data = definition.Data, Definition = definition, Sender = sender }; Notify(notification, recipients); }