<?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/"
	>

<channel>
	<title>Jamie Dixon &#187; c#</title>
	<atom:link href="http://www.jamie-dixon.co.uk/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jamie-dixon.co.uk</link>
	<description>Web Developer, Software Engineer and Mixed Language Artist</description>
	<lastBuildDate>Thu, 18 Aug 2011 09:31:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rendering a partial view n number of times with ASP.NET MVC</title>
		<link>http://www.jamie-dixon.co.uk/mvc/rendering-a-partial-view-n-number-of-times-with-asp-net-mvc/</link>
		<comments>http://www.jamie-dixon.co.uk/mvc/rendering-a-partial-view-n-number-of-times-with-asp-net-mvc/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 09:31:15 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[asp.net-mvc2]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[PartialViews]]></category>
		<category><![CDATA[RenderPartial]]></category>

		<guid isPermaLink="false">http://www.jamie-dixon.co.uk/?p=232</guid>
		<description><![CDATA[UPDATE
After spending a little more time working with MVC2 and having read this comment by Brad Wilson, I now know about the existence of Html.Partial which renders a partia view directly to a string.
This simplifies my code in a number of ways. Firstly the code itself is just simpler. There&#8217;s no longer any need to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE</strong></p>
<p>After spending a little more time working with MVC2 and having read <a href="http://stackoverflow.com/questions/1283079/how-does-the-html-helper-renderpartial-work-how-can-i-implement-a-helper-that#answer-1283690">this comment</a> by Brad Wilson, I now know about the existence of Html.Partial which renders a partia view directly to a string.</p>
<p>This simplifies my code in a number of ways. Firstly the code itself is just simpler. There&#8217;s no longer any need to render the partial out to a string because our new Partial method does this for us.</p>
<p>The RenderPartials method now looks like:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p232code6'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2326"><td class="code" id="p232code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> RenderPartials<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper helper, <span style="color: #6666cc; font-weight: bold;">string</span> partialViewName, IEnumerable models, <span style="color: #6666cc; font-weight: bold;">string</span> htmlFormat<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var view <span style="color: #0600FF; font-weight: bold;">in</span> models<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>model <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> helper<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Partial</span><span style="color: #008000;">&#40;</span>partialViewName,model<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                helper<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">HttpContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Output</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>htmlFormat, view<span style="color: #008000;">.</span><span style="color: #0000FF;">ToHtmlString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Much cleaner !!</p>
<p>This also fixed up the problem of using a names partial view rather than the full path. Again, all of this is handled by our new Partial method which leaves us get on with the important stuff.</p>
<p>Thanks again to the MVC team for including something extremely useful in MVC2.</p>
<p><strong>Origional Post</strong></p>
<p>I&#8217;ve recently been thinking about the way that RenderPartial works in ASP.NET MVC and one of the things I&#8217;ve been wanting is a way to render a partial view n number of times based on some kind of collection.</p>
<p>I had a look around and didn&#8217;t find anything so I&#8217;ve  written out a solution which I&#8217;d like to share. It&#8217;s by no means a perfect solution right now. I&#8217;d like to add support for named partials rather than specifying the path to the partial view and this will be part of the next iteration.</p>
<p><strong>Why?</strong><br />
The main reason for this solution is to solve the problem of having looping code in the views. This can quite often get a bit messy especially when mixing html and C#.</p>
<p><strong>The implementation</strong></p>
<p>Firstly, here&#8217;s the method signature:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p232code7'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2327"><td class="code" id="p232code7"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> RenderPartials<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper helper,
                                                 <span style="color: #6666cc; font-weight: bold;">string</span> partialViewName,
                                                 IEnumerable models,
                                                 <span style="color: #6666cc; font-weight: bold;">string</span> htmlFormat<span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<ul>
<li>The partialViewName parameter is going to be the location of our partial view. I&#8217;m going to be improving this to accept just the name of the view in this next iteration.</li>
<li>The models parameter contains a collection and for each item in that collection, we&#8217;ll render out our partial view.</li>
<li>The htmlFormat paramter is an optional parameter. This basically allows you to specify code that you&#8217;d like to be part of the iteration of each model item with {0} being the partial view itself (I&#8217;ll give an example below)</li>
</ul>
<p><strong>The method:</strong></p>
<p>So here&#8217;s the method as it stands at the moment.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p232code8'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2328"><td class="code" id="p232code8"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> RenderPartials<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper helper, <span style="color: #6666cc; font-weight: bold;">string</span> partialViewName, IEnumerable models, <span style="color: #6666cc; font-weight: bold;">string</span> htmlFormat<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            htmlFormat <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>htmlFormat<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;{0}&quot;</span> <span style="color: #008000;">:</span> htmlFormat<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var model <span style="color: #0600FF; font-weight: bold;">in</span> models<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                var viewDataDictionary <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ViewDataDictionary<span style="color: #008000;">&#40;</span>model<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                var urlHelper <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UrlHelper<span style="color: #008000;">&#40;</span>helper<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RequestContext</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                var x <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ViewPage <span style="color: #008000;">&#123;</span>ViewData <span style="color: #008000;">=</span> viewDataDictionary, Url <span style="color: #008000;">=</span> urlHelper, ViewContext <span style="color: #008000;">=</span> helper<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewContext</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
                Control control <span style="color: #008000;">=</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadControl</span><span style="color: #008000;">&#40;</span>partialViewName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                x<span style="color: #008000;">.</span><span style="color: #0000FF;">Controls</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>control<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                var sb <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var sw <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StringWriter<span style="color: #008000;">&#40;</span>sb<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var tw <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> HtmlTextWriter<span style="color: #008000;">&#40;</span>sw<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #008000;">&#123;</span>
                        x<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderControl</span><span style="color: #008000;">&#40;</span>tw<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                        helper<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">HttpContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Output</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>htmlFormat,sb<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Output content to the response stream</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>with a simple overload method to make the htmlFormat parameter optional:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p232code9'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2329"><td class="code" id="p232code9"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> RenderPartials<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper helper, <span style="color: #6666cc; font-weight: bold;">string</span> partialViewName, IEnumerable models<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            helper<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderPartials</span><span style="color: #008000;">&#40;</span>partialViewName,models,<span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong>How do we use it?</strong></p>
<p>Here&#8217;s a basic example of how we can use this new RenderPartials method within one of our views:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p232code10'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23210"><td class="code" id="p232code10"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;%:</span> Html<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderPartials</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;~/Areas/OrderManagement/Views/Shared/Order.ascx&quot;</span>,
                                   Model<span style="color: #008000;">.</span><span style="color: #0000FF;">Orders</span>,
                                   <span style="color: #666666;">&quot;&lt;span class=&quot;</span>\<span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>specialOrder\<span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span><span style="color: #666666;">&quot;&gt;{0}&lt;/span&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">%&amp;</span>gt<span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>This will now render our partial view Order.ascx once for each order in Model.Orders. Our third parameter for htmlFormat specifies some html that we&#8217;re wrapping our partial view in with {0} being the partial view itself. This allows for our front end team to add custom html to specific renderings of our partial view.</p>
<p><strong>Conclusion</strong><br />
So the main idea of this method is to neaten up our views a little bit whilst at the same time, giving our front end guys enough control and flexibility to make the solution viable.</p>
<p>If you have any comments or sugestions you can either drop me a line at jamie@jamie-dixon.co.uk or leave a comment below.</p>
<p><strong>You can follow me on Twitter at <a href="http://twitter.com/jamiedixon">http://twitter.com/jamiedixon</a> <img src='http://www.jamie-dixon.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamie-dixon.co.uk/mvc/rendering-a-partial-view-n-number-of-times-with-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Global Resource strings by property</title>
		<link>http://www.jamie-dixon.co.uk/concepts/global-resource-strings-by-property/</link>
		<comments>http://www.jamie-dixon.co.uk/concepts/global-resource-strings-by-property/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 15:10:44 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[concepts]]></category>
		<category><![CDATA[App_GlobalResources]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[resources]]></category>

		<guid isPermaLink="false">http://www.jamie-dixon.co.uk/?p=208</guid>
		<description><![CDATA[Every now and then I come across a bunch of string literals littered through out some code and begin the task of moving them out into their own resource file (.resx).
Gaining access to these strings from C# is pretty easy however I&#8217;m yet to find anywhere explaining it the way I find simplest.
Assuming your resx [...]]]></description>
			<content:encoded><![CDATA[<p>Every now and then I come across a bunch of string literals littered through out some code and begin the task of moving them out into their own resource file (.resx).</p>
<p>Gaining access to these strings from C# is pretty easy however I&#8217;m yet to find anywhere explaining it the way I find simplest.</p>
<p>Assuming your resx file is stored in the App_GlobalResources folder, you can basically assign your resource file to a variable in your C# using the following syntax:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p208code13'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20813"><td class="code" id="p208code13"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Res </span><span style="color: #008000;">=</span> Resources<span style="color: #008000;">.</span><span style="color: #0000FF;">MyResourceFile</span></pre></td></tr></table></div>

<p>This then gives you the ability to referene the contents of the resource file as properties in your code.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p208code14'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20814"><td class="code" id="p208code14"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">string</span> x <span style="color: #008000;">=</span> Res<span style="color: #008000;">.</span><span style="color: #0000FF;">OneOfMyStringNames</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jamie-dixon.co.uk/concepts/global-resource-strings-by-property/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

