D'Arcy Rittich
D'Arcy Rittich

Reputation: 171421

Would you use this ASP.NET MVC view syntax?

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.

Upvotes: 1

Views: 664

Answers (6)

yfeldblum
yfeldblum

Reputation: 65435

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>

Upvotes: 1

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99730

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.

Upvotes: 0

Dale Ragan
Dale Ragan

Reputation: 18270

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.

Upvotes: 0

Daniel Schaffer
Daniel Schaffer

Reputation: 57852

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?

Upvotes: -2

Matt Briggs
Matt Briggs

Reputation: 42208

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

Upvotes: 1

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99730

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.

Upvotes: 2

Related Questions