Rendering a view to a string in MVC, then redirecting — workarounds?

后端 未结 3 1807
轻奢々
轻奢々 2021-01-14 03:14

I can\'t render a view to a string and then redirect, despite this answer from Feb (after version 1.0, I think) that claims it\'s possible. I thought I was doing something w

3条回答
  •  感动是毒
    2021-01-14 03:52

    Updated.

    Now that I understand that you want to use the view engine to generate the actual email in html, I propose the following:

    Code to render action to text within a controller: http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html

    Minor edits to your code:

    public ActionResult Certifications(string email_intro)
    {
         //a lot of stuff              
         ViewData["users"] = users;              
         if (isPost())             
         {                 
             //create the viewmodel                 
             var view_model = new ViewModels.Emails.Certifications.Open(userContext) { emailIntro = email_intro };                  
    
             foreach (var user in users)                 
             {                     
                 if (user.Email_Address.IsValidEmailAddress())                     
                 {                         
                     view_model.user = user;                         
                     view_model.certification302Summary.subProcessesOwner = new SubProcess_Certifications(RecordUpdating.Role.Owner, null, null, user.User_ID, repository);                         
    
                     SendEmail(view_model);                     
                 }                 
             }                  
             return RedirectToAction("Certifications");             
         }              
         return View();         
     } 
    
     public void SendEmail(ViewModels.Emails.Certifications.Open model)         
     {             
        var vd = context.Controller.ViewData;             
        vd["model"] = model;             
        var renderer = new CustomRenderers();             
    
        // Implement the actual email rendering as a regular action method on this controller
        var text = this.CaptureActionHtml(c => (ViewResult)c.RenderEmail(model));
    
        var a = text;         
    } 
    

提交回复
热议问题