Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft MVC "echo/print/output" etc

With ASP.NET's view engine/template aspx/ashx pages the way to spit to screen seems to be:

<%= Person.Name %>

Which was fine with webforms as alot of model data was bound to controls programatically. But with MVC we are now using this syntax more oftern.

The issue I have with it is quite trivial, but annoying either way. This is that it seems to break up the mark up i.e.:

<% foreach(var Person in People) { %>
    <%= Person.Name %>
<% } %>

That seems like alot of opening and closing tags to me!

Other view engines in the MVC contrib have a means of spitting to screen with out opening and closing the script tags using standard keyword such as "print, out, echo" i.e. (brail example):

<% 
for element in list:
      output "<li>${element}</li>" 
end
%>

Now, I said this may seem trivial, but it just seems more readable this way. So what are the advantages of MS having this syntax, and not providing a output method?

Cheers, Chris.

like image 238
Owen Avatar asked Oct 13 '08 13:10

Owen


2 Answers

Consider something like this, instead:

<% foreach(var Person in People) { 
    Response.Write(Person.Name); 
} %>

I believe that'll work. (Although I haven't tested it; I've only just begun with MVC and don't have the toolset here at the office.)

EDIT: I apparently missed the actual question ... :)

Microsoft does provide an output method, but didn't provide a syntax like the one you describe. The output method is Response.Write(). I can't answer this directly (I believe you'll need to check with Scott Hanselmann over at MS :)), but I think they didn't want to complicate scripting by adding yet-another-language for us to learn; I think they wanted to leverage the languages (C#, VB, etc.) which developers already knew.

EDIT #2: I placed the following in a comment, and in retrospect (for completeness), it should be part of the answer.

If you head over to the Learn MVC site on ASP.NET, in the view tutorial (that's the link), you'll see the following paragraph:

Since you call Response.Write() so often, Microsoft provides you with a shortcut for calling the Response.Write() method. The view in Listing 3 uses the delimiters <%= and %> as a shortcut for calling Response.Write().

Essentially, <%= %> is the accepted shortcut for Response.Write, and you therefore can use the full Response.Write method anywhere you'd use <%= %>.

like image 177
John Rudy Avatar answered Sep 25 '22 18:09

John Rudy


My answer is based on personal XP with MVC4 cshtml - you can use: @Html.Raw("SomeStringDirectlyInsideTheBrowserPageHTMLCode")

This renders the (dynamic) string at its position unlike Response.Write(MyString) which as far as I've noticed always renders the string at the begining of the browser-page.

Note that the HTML tags rendered by @Html.Raw(MyString) cannot be checked by the compiler. I mean: @Html.Raw("<div ....>") cannot be closed by mere </div> because you will get an error (<div ....> is not detected by the compiler) so you must close the tag with @Html.Raw("</div>")

P.S.
In some cases this doesn't work (for example it fails within DevExpress) - use ViewContext.Writer.Write() or ViewContext.Writer.WriteLine() instead.

like image 25
KurvaBG Avatar answered Sep 21 '22 18:09

KurvaBG