Would you use this ASP.NET MVC view syntax?
I have suggested what I think is the best syntax for ASP.NET MVC views in this question . Since this question has been answered, I think my answer will generate a little feedback, so I am posting it as my own question here.
source to share
You're on the right track, but I think you've gone too far. Balance mixes the code with the html where it flows rather than over complicating it and also doesn't create a soup tag. The best viewer I've found that does this is Spark .
Take a look at it and you will find what you are suggesting in a more subtle and readable way.
source to share
You are using markup to represent your code. My opinion is this: where code is needed, just use code that is always more flexible. If markup is required, use markup. This article explains exactly my point. Sometimes the line between code and markup is blurry.
source to share
Perhaps you should use this one instead of the MVC syntax called HAML.
%h2= Model.CategoryName
%ul
- foreach (var product in Model.Products)
%li
= product.ProductName
.editlink
= Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })
= Html.ActionLink("Add New Product", new { Action="New" })
replaces
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true"
CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %>
<asp:Content ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<h2><%= ViewData.Model.CategoryName %></h2>
<ul>
<% foreach (var product in ViewData.Model.Products) { %>
<li>
<%= product.ProductName %>
<div class="editlink">
(<%= Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })%>)
</div>
</li>
<% } %>
</ul>
<%= Html.ActionLink("Add New Product", new { Action="New" }) %>
</asp:Content>
source to share
Also take a look at the JSPs: they needed to introduce an "expression language" to get some code power in the jsp markup. The result is very inconvenient IMHO. It even requires an explicit mapping (in XML, of course) to access a simple function from that expression language.
See this .
source to share