title: ASP.net core: Model is null during rendering a page to string
body: <p>I want to render a page as email template.
  <br>
  But I get an error when the renderer wants to use the model
  <br>
  I follow this tutorial: <a href="https://www.learnrazorpages.com/advanced/render-partial-to-string" rel="nofollow noreferrer">learnrazorpages.com</a></p>
  <p>RazorRenderer.cs:</p>
  <pre><code>public class RazorRenderer : IRazorRenderer
  {
      private readonly IRazorViewEngine _viewEngine;
      private readonly ITempDataProvider _tempDataProvider;
      private readonly IServiceProvider _serviceProvider;
  
      public RazorRenderer(
          IRazorViewEngine viewEngine,
          ITempDataProvider tempDataProvider,
          IServiceProvider serviceProvider)
      {
          _viewEngine = viewEngine;
          _tempDataProvider = tempDataProvider;
          _serviceProvider = serviceProvider;
      }
  
      public async Task&lt;string&gt; ToStringAsync&lt;TModel&gt;(string partialName, TModel model)
      {
          var actionContext = GetActionContext();
          var partial = FindView(actionContext, partialName);
          var viewData = new ViewDataDictionary&lt;TModel&gt;(new EmptyModelMetadataProvider(), new ModelStateDictionary());
          var tempData = new TempDataDictionary(actionContext.HttpContext, _tempDataProvider);
  
          viewData.Model = model;
  
          await using var stringWriter = new StringWriter();
  
          var viewContext = new ViewContext(
              actionContext,
              partial,
              viewData,
              tempData,
              stringWriter,
              new HtmlHelperOptions()
          );
  
          await partial.RenderAsync(viewContext);
  
          return stringWriter.ToString();
      }
  
      private IView FindView(ActionContext actionContext, string partialName)
      {
          var getPartialResult = _viewEngine.GetView(null, partialName, false);
  
          if (getPartialResult.Success)
          {
              return getPartialResult.View;
          }
  
          var findPartialResult = _viewEngine.FindView(actionContext, partialName, false);
  
          if (findPartialResult.Success)
          {
              return findPartialResult.View;
          }
  
          var searchedLocations = getPartialResult.SearchedLocations.Concat(findPartialResult.SearchedLocations);
          var errorMessage = string.Join(
              Environment.NewLine,
              new[]
              {
                  $&quot;Unable to find partial '{partialName}'. The following locations were searched:&quot;
              }.Concat(searchedLocations));
  
          throw new InvalidOperationException(errorMessage);
      }
  
      private ActionContext GetActionContext()
      {
          var httpContext = new DefaultHttpContext
          {
              RequestServices = _serviceProvider
          };
  
          return new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
      }
  }
  </code></pre>
  <p>Test.cs:</p>
  <pre><code>public class TestModel : PageModel
  {
      private readonly IRazorRenderer _razorRenderer;
  
      public RecoveryModel(IRazorRenderer razorRenderer)
      {
          _razorRenderer = razorRenderer;
      }
  
      public void OnGet()
      {
      }
  
      public async Task&lt;IActionResult&gt; OnPost()
      {
          var body = await _razorRenderer.ToStringAsync(
              &quot;Email&quot;,
              new Test
              {
                  Email = &quot;Email@Test.com&quot;
              });
  
          return Content(body);
      }
  }
  </code></pre>
  <p>Email.cshtml.cs</p>
  <pre><code>public class EmailModel : PageModel
  {
      public string Email { get; set; }
  
      public void OnGet()
      {
      }
  }
  </code></pre>
  <p>Email.cshtml:</p>
  <pre><code>@page
  @model EmailModel
  @{
      Layout = null;
  }
  @Model.Email // Here I get the exception (Model is null)
  </code></pre>
  <p>Where did I do the wrong thing?</p>