Render MVC ViewModel without turnkey
This page was created by Hans.karlsen@mdriven.net on 2019-04-22. Last edited by Edgar on 2025-01-20.

MDriven Turnkey does a lot to get a seamless environment for application development.

If you would like to make use of parts of the functionality and render a specific viewmodel you can do so.

This is how turnkey does it internally:

  internal static class MvcRazorPartialViewShared
  {
    internal static string RenderRazorPartial(ViewModel.Runtime.ViewModel viewModel)
    {
      StringBuilder sb = new StringBuilder(2048); // Building big things!
                                                  // IMPORTANT! The Using and statements should match the one in the main page!
      sb.AppendLine("@using Eco.ViewModel.Runtime");
      sb.AppendLine("@using Eco.MVC");
      sb.AppendLine("@using StreamingAppGenericAPIAndControllers");
      sb.AppendLine("@model VMClass");
      RazorControlRenderer razorRenderer = new RazorControlRenderer();
      if (RenderSettings.CheckUseCSSGrid(viewModel))
      {
        CSSGridViewUICreator cssGridViewCreator = new CSSGridViewUICreator(razorRenderer, sb);
        ViewModelRTCreator.CreateDataAndUI(null, cssGridViewCreator, viewModel); 
      }
      else
      {
        BootstrapViewUICreator bootstrapViewCreator = new BootstrapViewUICreator(razorRenderer, sb);
        ViewModelRTCreator.CreateDataAndUI(null, bootstrapViewCreator, viewModel); // Create UI delegating work to UICreator
      }
      return sb.ToString();
    }
  }