Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designer-friendly views in Asp.Net MVC

I'm enjoying Asp.Net MVC and am looking to use it in an upcoming project. Part of the project, however, is an emphasis on being able to expose the project Views to designers for things like theming and so on. One problem I'm anticipating is that Asp.Net MVC views are rather developer-centric. I really don't want to have to educate designers on the intracies of <% vs. <%= let alone something like <% foreach ...

Take a typical MVC menu structure, for example.

<div id="menu">
 <ul>
      <li><%= Html.ActionLink("Home", "Index", "Main")%></li>
      <li><%= Html.ActionLink("About", "About", "Main")%></li>
      <li><% Html.RenderPartial("LogOnUserControl"); %></li>
  </ul>
</div>

I'd much rather be able to tell designers to go with something like

<div id="menu">
 <ul>
      <li>{ActionLink "Home", "Index", "Main"}</li>
      <li>{ActionLink "About", "About", "Main"}</li>
      <li>{Partial "LogOnUserControl"}</li>
  </ul>
</div>

Or

<div id="menu">
 <ul>
      <li><my:ActionLink text="Home" action="Index" controller="Main" /></li>
      <li><my:ActionLink text="About" action="About" controller="Main" /></li>
      <li><my:Partial name="LogOnUserControl" /></li>
  </ul>
</div>

Yes, that last looks suspiciously like a raft of UserControls. Personally, I'm not a fan of actually using UserControls to do this if only because the rendering of those controls happens after pretty much everything else (as I understand it) and I'd prefer something that fits more in line with the MVC lifecycle. All I really need is a set of placeholders and a way to replace them with the relevant rendering.

So where's the best place to do so and what kind of trade-offs am I looking at here. I can imagine a couple of angles to come at this:

  • A custom ViewPage class where I can override something relevant. ViewPage.RenderView or ViewPage.FrameworkInitialize, maybe, but how you get at the text from there I don't know.
  • Create a custom TextWriter and override ViewPage.CreateHtmlTextWriter that I can then intercept the text output for replacing stuff. This is pretty late in the cycle, though, and will mess with other custom filtering if I'm not careful.
  • Create my own IView and ViewEngine classes. I didn't get far down this path before wondering if I was headed to a very bad place.
  • Custom UserControls that can mimic the functionality needed.

Opinions? Other options? Is my own ViewEngine my best option? My own ViewPage? Or are UserControl objects going to be adequate (please say no)?

like image 609
Jacob Proffitt Avatar asked Oct 29 '09 21:10

Jacob Proffitt


People also ask

What is ViewModel in MVC with example?

ViewModel = Model that is created to serve the view. ASP.NET MVC view can't have more than one model so if we need to display properties from more than one model in the view, it is not possible. ViewModel serves this purpose. View Model is a model class that can hold only those properties that are required for a view.

How can add Cshtml file in ASP.NET MVC?

Right click the Views\HelloWorld folder and click Add, then click MVC 5 View Page with Layout (Razor). In the Specify Name for Item dialog box, enter Index, and then click OK. In the Select a Layout Page dialog, accept the default _Layout. cshtml and click OK.

Which of the following is a type of view in MVC?

On basis of data transfer mechanism ASP.NET MVC views are categorized as two types, Dynamic view. Strongly typed view.

What is Cshtml in MVC?

What is a CSHTML file? A file with . cshtml extension is a C# HTML file that is used at server side by Razor Markup engine to render the webpage files to user's browser.


2 Answers

Take a look at Spark. It's syntax is similar to your example.

like image 91
Chuck Conway Avatar answered Sep 21 '22 14:09

Chuck Conway


As Charles Conway said, Spark is definitely way to go. Look at example. First, you create partial view in _ActionLink.spark with code:

<viewdata text="String" action="String" controller="String">
${ Html.ActionLink(controller, action, text) }

Then you use it like that in other views:

<ActionLink text="Home" action="Index" controller="Main" />

You just have to prepare partial views for your designers and then they an create view in prefered style. Isn't it easy?

EDIT:

Sorry, that was wrong. <ActionLink text="Home" action="Index" controller="Main" /> will not work, because Home, Index and Main are treates as variables. It may not be that easy to do it as you wish.

like image 24
LukLed Avatar answered Sep 18 '22 14:09

LukLed