<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>render &#8211; MaxVergelli.com</title>
	<atom:link href="https://www.maxvergelli.com/tag/render/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.maxvergelli.com</link>
	<description>sourcecode repository</description>
	<lastBuildDate>Sat, 11 Apr 2020 15:54:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i2.wp.com/www.maxvergelli.com/wp-content/uploads/2020/04/wp-1586823685141.png?fit=32%2C32&#038;ssl=1</url>
	<title>render &#8211; MaxVergelli.com</title>
	<link>https://www.maxvergelli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">34603511</site>	<item>
		<title>Render a Section in a Partial View &#8211; ASP.Net MVC</title>
		<link>https://www.maxvergelli.com/render-a-section-in-a-partial-view-asp-net-mvc/</link>
					<comments>https://www.maxvergelli.com/render-a-section-in-a-partial-view-asp-net-mvc/#respond</comments>
		
		<dc:creator><![CDATA[Max]]></dc:creator>
		<pubDate>Sun, 12 Apr 2020 15:34:00 +0000</pubDate>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[VB.Net]]></category>
		<category><![CDATA[asp.net-mvc]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[mvc-partial-view]]></category>
		<category><![CDATA[mvc-project]]></category>
		<category><![CDATA[mvc-view]]></category>
		<category><![CDATA[partial-view]]></category>
		<category><![CDATA[razor]]></category>
		<category><![CDATA[render]]></category>
		<category><![CDATA[section]]></category>
		<category><![CDATA[vb.net]]></category>
		<guid isPermaLink="false">https://www.maxvergelli.com/?p=231</guid>

					<description><![CDATA[Let&#8217;s start with the following configuration to give an example: in a MVC project, you have a &#8220;_Layout.vbhtml&#8221; file with this code Then, you have a Partial View &#8220;ValidationScripts.vbhtml&#8221; in the Shared folder with If you call the Partial View &#8230; <a class="kt-excerpt-readmore" href="https://www.maxvergelli.com/render-a-section-in-a-partial-view-asp-net-mvc/" aria-label="Render a Section in a Partial View &#8211; ASP.Net MVC">Read More</a>]]></description>
										<content:encoded><![CDATA[
<p>Let&#8217;s start with the following configuration to give an example: in a MVC project, you have a &#8220;_Layout.vbhtml&#8221; file with this code</p>



<div class="wp-block-codemirror-blocks-code-block alignfull code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;vb&quot;,&quot;mime&quot;:&quot;text/x-vb&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;full&quot;,&quot;language&quot;:&quot;VB.NET&quot;,&quot;modeName&quot;:&quot;vb&quot;}">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
	&lt;!-- some code here --&gt;
    &lt;script src=&quot;@Url.Content(&quot;~/Scripts/jquery-1.8.2.min.js&quot;)&quot;&gt;		&lt;/script&gt;
    @RenderSection(&quot;Scripts&quot;, false)
  &lt;/body&gt;
&lt;/html&gt;</pre></div>



<p>Then, you have a Partial View &#8220;ValidationScripts.vbhtml&#8221; in the Shared folder with</p>



<div class="wp-block-codemirror-blocks-code-block alignfull code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;vb&quot;,&quot;mime&quot;:&quot;text/x-vb&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;full&quot;,&quot;language&quot;:&quot;VB.NET&quot;,&quot;modeName&quot;:&quot;vb&quot;}">@Section Scripts
    &lt;script src=&quot;@Url.Content(&quot;~/Scripts/jquery.validate.min.js&quot;)&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;@Url.Content(&quot;~/Scripts/jquery.validate.unobtrusive.min.js&quot;)&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;@Url.Content(&quot;~/Scripts/jquery.validate.fix.js&quot;)&quot;&gt;&lt;/script&gt;  
    &lt;script src=&quot;@Url.Content(&quot;~/Scripts/localization/messages_de.js&quot;)&quot;&gt;&lt;/script&gt;      
End Section</pre></div>



<p>If you call the Partial View from a View like this…</p>



<div class="wp-block-codemirror-blocks-code-block alignfull code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;vb&quot;,&quot;mime&quot;:&quot;text/x-vb&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;full&quot;,&quot;language&quot;:&quot;VB.NET&quot;,&quot;modeName&quot;:&quot;vb&quot;}">@ModelType MvcExample.MyModel
@Code
    ViewData(&quot;Title&quot;) = &quot;Test&quot;
End Code

@Html.Partial(&quot;ValidationScripts&quot;)

&lt;h2&gt;Just a Test&lt;/h2&gt;</pre></div>



<p>However the Section &#8220;Scripts&#8221; is not rendered on the page, and the output HTML will be&#8230;</p>



<div class="wp-block-codemirror-blocks-code-block alignfull code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;vb&quot;,&quot;mime&quot;:&quot;text/x-vb&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;full&quot;,&quot;language&quot;:&quot;VB.NET&quot;,&quot;modeName&quot;:&quot;vb&quot;}">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
    
    &lt;script src=&quot;@Url.Content(&quot;~/Scripts/jquery-1.8.2.min.js&quot;)&quot;&gt;&lt;/script&gt;

  &lt;/body&gt;
&lt;/html&gt;</pre></div>



<p><strong>Solution</strong></p>



<p>To render the Section in the Partial View, you need to create a couple of Extension methods like this:</p>



<div class="wp-block-codemirror-blocks-code-block alignfull code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;vb&quot;,&quot;mime&quot;:&quot;text/x-vb&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;full&quot;,&quot;language&quot;:&quot;VB.NET&quot;,&quot;modeName&quot;:&quot;vb&quot;}">public static class HtmlHelperExtensions
{
  private const string _jSViewDataName = &quot;RenderJavaScript&quot;;
  private const string _styleViewDataName = &quot;RenderStyle&quot;;

  public static void AddJavaScript(this HtmlHelper htmlHelper, 
                                   string scriptURL)
  {
    List&lt;string&gt; scriptList = htmlHelper.ViewContext.HttpContext
      .Items[HtmlHelperExtensions._jSViewDataName] as List&lt;string&gt;;
    if (scriptList != null)
    {
      if (!scriptList.Contains(scriptURL))
      {
        scriptList.Add(scriptURL);
      }
    }
    else
    {
      scriptList = new List&lt;string&gt;();
      scriptList.Add(scriptURL);
      htmlHelper.ViewContext.HttpContext
        .Items.Add(HtmlHelperExtensions._jSViewDataName, scriptList);
    }
  }

  public static MvcHtmlString RenderJavaScripts(this HtmlHelper HtmlHelper)
  {
    StringBuilder result = new StringBuilder();

    List&lt;string&gt; scriptList = HtmlHelper.ViewContext.HttpContext
      .Items[HtmlHelperExtensions._jSViewDataName] as List&lt;string&gt;;
    if (scriptList != null)
    {
      foreach (string script in scriptList)
      {
        result.AppendLine(string.Format(
          &quot;&lt;script type=\&quot;text/javascript\&quot; src=\&quot;{0}\&quot;&gt;&lt;/script&gt;&quot;, 
          script));
      }
    }

    return MvcHtmlString.Create(result.ToString());
  }

  public static void AddStyle(this HtmlHelper htmlHelper, string styleURL)
  {
    List&lt;string&gt; styleList = htmlHelper.ViewContext.HttpContext
      .Items[HtmlHelperExtensions._styleViewDataName] as List&lt;string&gt;;

   if (styleList != null)
   {
     if (!styleList.Contains(styleURL))
     {
       styleList.Add(styleURL);
     }
   }
   else
   {
     styleList = new List&lt;string&gt;();
     styleList.Add(styleURL);
     htmlHelper.ViewContext.HttpContext
       .Items.Add(HtmlHelperExtensions._styleViewDataName, styleList);
   }
 }

 public static MvcHtmlString RenderStyles(this HtmlHelper htmlHelper)
 {
   StringBuilder result = new StringBuilder();

   List&lt;string&gt; styleList = htmlHelper.ViewContext.HttpContext
     .Items[HtmlHelperExtensions._styleViewDataName] as List&lt;string&gt;;

   if (styleList != null)
   {
     foreach (string script in styleList)
     {
       result.AppendLine(string.Format(
         &quot;&lt;link href=\&quot;{0}\&quot; rel=\&quot;stylesheet\&quot; type=\&quot;text/css\&quot; /&gt;&quot;, 
         script));
     }
   }

  return MvcHtmlString.Create(result.ToString());
  }
}</pre></div>



<p>So, on any View or Partial View or Display/Edit Template you simply add what you need:</p>



<div class="wp-block-codemirror-blocks-code-block alignfull code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;vb&quot;,&quot;mime&quot;:&quot;text/x-vb&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;full&quot;,&quot;language&quot;:&quot;VB.NET&quot;,&quot;modeName&quot;:&quot;vb&quot;}">@{
  Html.AddJavaScript(&quot;http://cdn.jquerytools.org/1.2.7/full/jquery.tools.min.js&quot;);
}</pre></div>



<p>Then, in your Layouts you render it where you want it! Like this markup&#8230;</p>



<div class="wp-block-codemirror-blocks-code-block alignfull code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;vb&quot;,&quot;mime&quot;:&quot;text/x-vb&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;full&quot;,&quot;language&quot;:&quot;VB.NET&quot;,&quot;modeName&quot;:&quot;vb&quot;}">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
  @Html.RenderStyles()
  @Html.RenderJavascripts()</pre></div>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.maxvergelli.com/render-a-section-in-a-partial-view-asp-net-mvc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">231</post-id>	</item>
	</channel>
</rss>
