title: Generating HTML DOM Element and inserting it into a Razor Page
body: <p>I currently am working on a asp.net MVC web application in which I have to generate a more or less complex HTML structure and then insert it into a Razor Page. Currently I generate the HTML with string concatenation and then add the string to the ViewBag in my controller, like this:</p>
  <pre><code>public class SomeController : Controller
  {
      public IActionResult Index()
      {
          string htmlString = &quot;&lt;div id=\&quot;example-div\&quot;&gt;Some more Stuff&lt;/div&gt;&quot; + &quot;Some more HTML strings&quot;;
          Viewbag.htmlString = htmlString;
              
          return View();
      }
  }
  </code></pre>
  <p>And then I access and render that String into HTML inside of my Razor Page like this:</p>
  <pre><code>@Html.Raw(ViewBag.htmlString)
  </code></pre>
  <p>But this can't be best practice. Is there a better way to actually generate a HTML DOM element inside my controller and then load that element into the razor page like a template? If so how?</p>
  <p>Thanks</p>