Question

I proposed what I think is a better syntax for ASP.NET MVC views in this question. Since that question has been answered, I think my answer will generate little feedback, so I am posting it as its own question here.

Was it helpful?

Solution

You're on the right track, but I think you went too far. The balance is mixing the code with the html where it flows and not over complicating it and also not creating tag soup. The best view engine that I have found that does this is Spark.

Take a look at it and you will find it addresses what you are proposing in a more subtle and readable way.

OTHER TIPS

You're using markup to represent code. My opinion is: where code is needed, just use code, which is always more flexible. Where markup is needed, use markup. This article explains precisely my point. Sometimes the line between code and markup is blurry, though.

I really wish that people would stop treating XML as a programming language.

Maybe you should use this "MVC syntax" instead, 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>

Also take a look at JSP: they had to introduce an "Expression Language" in order to get some of the power of code in the jsp markup. The result is really awkward IMHO. It even needs an explicit mapping (in XML, of course) to access a simple function from this Expression Language.

See this.

In addition to maucsch and matt's points, wouldn't this also mean that the server would have to load into memory and parse the entire page looking for "mvc:"? And isn't that one of the reasons to not use webforms?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow