Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've managed to figure something out. Although my problem is not exactly the same you have, but it can certainly help you. But first things first.</p> <p><strong>The problem</strong></p> <p>I have a number of controls (in my case they are Partial Views). For example:</p> <pre><code>Views/Shared/EditName.cshtml Views/Shared/EditAddress.cshtml Views/Shared/EditEmail.cshtml Views/Shared/EditFavoriteColour.cshtml </code></pre> <p>I want to define groups of these controls (in database or web.config), for example:</p> <pre><code>SimpleEditGroup: EditName ExtendedEditGroup: EditName EditEmail FullEditGroup: EditName EditAddress EditEmail EditFavoriteColour </code></pre> <p>Then I want to render a view based on a particular group. I was thinking the following syntax would be sufficient.</p> <pre><code>@Html.Partial("ControlGroup:ExtendedEditGroup", Model) </code></pre> <p><strong>Draft of the solution</strong></p> <p>I wanted to create my own render engine which will do the following:</p> <ol> <li>If the name of the view begins with "ControlGroup:" then use previously defined groups to render the page. Each of the controls should be rendered by RazorViewEngine.</li> <li>If the name of the view doesn't begin with "ControlGroup:" just let the RazorViewEngine do its work.</li> </ol> <p><strong>Research</strong></p> <p>I've found following resources:</p> <ol> <li>Article on CodeProject - <a href="http://www.codeproject.com/Articles/259365/Custom-ViewEngine-in-ASP-NET-MVC3" rel="nofollow">Custom ViewEngine in ASP.NET MVC3</a></li> <li>Article based on StackOverflow question - <a href="http://www.paulstovell.com/widgets" rel="nofollow">Creating a Widget system in ASP.NET MVC 3</a></li> <li>I recommend you the book <a href="http://rads.stackoverflow.com/amzn/click/1430234040" rel="nofollow">Pro ASP.NET MVC 3 Framework</a>. It will give you the idea how many things you can customize in ASP.NET MVC 3</li> <li>Of course source code of <a href="http://aspnet.codeplex.com/releases/view/58781" rel="nofollow">ASP.NET MVC 3 RTM</a> was very helpful.</li> </ol> <p><strong>Final solution</strong></p> <p>MyViewEngine:</p> <pre><code>public class MyViewEngine : RazorViewEngine { IDictionary&lt;string, List&lt;string&gt;&gt; groups = new Dictionary&lt;string, List&lt;string&gt;&gt;(); public MyViewEngine() { // Temporary data source groups["SimpleEditGroup"] = new List&lt;string&gt;() { "EditName"}; groups["ExtendedEditGroup"] = new List&lt;string&gt;() { "EditName", "EditEmail"}; } public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { if (viewName.StartsWith("ControlGroup")) { var groupName = viewName.Split(':')[1]; var controls = new List&lt;ViewEngineResult&gt;(); foreach (var controlName in groups[groupName]) { // Find each control using Razor magic var control = base.FindPartialView(controllerContext, controlName, useCache); if (control.View != null) { controls.Add(control); } } if(controls.Count &gt; 0) return new ViewEngineResult(new MyView(controls), this); } return base.FindView(controllerContext, viewName, masterName, useCache); } } </code></pre> <p>MyView.cs:</p> <pre><code>public class MyView : IView { IList&lt;ViewEngineResult&gt; controls; public MyView(IList&lt;ViewEngineResult&gt; _controls) { controls = _controls; } public void Render(ViewContext viewContext, TextWriter textWriter) { // For simplicity I used table for layout textWriter.Write("&lt;table border='1'&gt;"); foreach (var ctrl in controls) { textWriter.Write("&lt;tr&gt;&lt;td&gt;"); // Render control using Razor ctrl.View.Render(viewContext, textWriter); textWriter.Write("&lt;/td&gt;&lt;/tr&gt;"); } textWriter.Write("&lt;/table&gt;"); } } </code></pre> <p>Don't forget to register your new engine in Global.asax <code>ViewEngines.Engines.Add(new MyViewEngine());</code></p> <p><strong>Happy ending</strong></p> <p>I can use this in two ways: embedded in a view using <code>Render.Partial</code> or just returning <code>PartialView</code> from my controller.</p> <p>Let me know if it helps you. And also you can share your solution.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload