Rendering views dynamically http://forums.asp.net/t/1792039.aspx/1?Rendering+views+dynamically+Thu, 12 Apr 2012 12:48:02 -040017920394928868http://forums.asp.net/p/1792039/4928868.aspx/1?Rendering+views+dynamically+Rendering views dynamically <p>I have two views</p> <p>-One is drawing List Box to select record-&quot;SelectionVer1.cshtml&quot;</p> <p>-2nd is drawing HTML Table with checkbox to select a record-&nbsp;&quot;SelectionVer2.cshtml&quot;</p> <p>(Both are using same model and same action method of a controller)</p> <p>Now I need to dynamically render this based on the logged in User. If user have roll1 then SelectionVer2.cshtml&nbsp;will be rendered by same controller otherwise&nbsp;SelectionVer1.cshtml will be rendered by same controller.</p> <p>How this can be achieved?</p> <p></p> 2012-04-12T10:47:29-04:004928893http://forums.asp.net/p/1792039/4928893.aspx/1?Re+Rendering+views+dynamically+Re: Rendering views dynamically <p>try use something like this</p> <pre class="prettyprint">&lt;html&gt; ....... @if(Request.User.Identity.IsInRole(&quot;Admin&quot;)) { @Html.RenderPartial(&quot;View1&quot;,Model); } else { @Html.RenderPartial(&quot;View2&quot;,Model); } ..... &lt;/html&gt;</pre> <p><br> <br> </p> <p></p> 2012-04-12T10:59:23-04:004928894http://forums.asp.net/p/1792039/4928894.aspx/1?Re+Rendering+views+dynamically+Re: Rendering views dynamically <pre class="prettyprint">public ActionResult Index() { if( HttpContext.User.Identity.Name == 'User1') { return View(&quot;View1&quot;); } if( HttpContext.User.Identity.Name == 'User2') { return View(&quot;View2&quot;); } //return a default view return View(); }</pre> <p><br> &nbsp;</p> 2012-04-12T10:59:43-04:004928923http://forums.asp.net/p/1792039/4928923.aspx/1?Re+Rendering+views+dynamically+Re: Rendering views dynamically <p>The user-view association is stored in database i.e. this is dynamic.</p> <p>There is&nbsp;possibility of adding new views as view library is another project.</p> <p>I can not hard code User Name and Views, as the association may be changed, i need approach like&nbsp;Dependency&nbsp;Injection, we are using this for injecting Model, but I am stuck for Views.</p> <p></p> 2012-04-12T11:12:18-04:004928934http://forums.asp.net/p/1792039/4928934.aspx/1?Re+Rendering+views+dynamically+Re: Rendering views dynamically <p>So, my suggestion based on Roles. It means you don't need to hardcode username.</p> <p>If your Views will change you can code something like version control that will return actual associated Views names.</p> 2012-04-12T11:16:37-04:004929138http://forums.asp.net/p/1792039/4929138.aspx/1?Re+Rendering+views+dynamically+Re: Rendering views dynamically <p>Thanks for the concept, we have implemented in below manner.</p> <pre class="prettyprint">public ActionResult Index() { bm = bd.GetBMCs(5); //using static class VersionControl to get components for user return View(VersionControl.GetComponent(HttpContext.User.Identity.Name, ComponentName.RangePlanConfigSelection), null, bm); }</pre> <p>Its working !<br> <br> </p> 2012-04-12T12:48:02-04:00