What is difference between these tags <% <%: <%= in ASP.NET MVC 2? - asp.net-mvc-2

The title contain my whole question.

<% /* Is a codeblock */ for(int i = 0;i<5;i++) { } %>
<%= "Writes something to the output stream" /* Response.Write */ %>
<%: "HTML-encodes this <b>hello</b> to the output stream" %>

For a good explanation about the <%, <%= and <%# syntax and their usage, please read this article.
The <%: syntax is new in .Net 4 and is used for encoding HTML output. See this article of ScottGu for more information about that.

<% %> is used just to execute server side code
ex. <% if(oject){...} %>
<%= %> is used execute server side code and return value
ex. <%=Html.Encode(Item["Name"]) %>
<%: %> is used execute server side code but it will return Html
Encoded string
ex. <%Item["Name"] %>
Source : What is difference between these tags <%, <%: , and <%= in ASP.NET MVC 2?

Related

Simple foreach loop in MVC

<% foreach (var car in Model.AvailableCars)
{ %>
<label><%car.Text; %></label>
<% } %>
The above code throws the error
Only assignment, call, increment, decrement, and new object expressions can be used as a statement.
I know I can do it with html helpers, but why won't the above code work?
<label><%car.Text; %></label>
should read
<label><%= car.Text; %></label>
^
or you can use
<label><%: car.Text; %></label>
^
which will automatically HTML.Encode the value for you.
Add a colon to the car.Text tag to write it to the document, such as:
<label><%: car.Text %></label>
Here's a good explanation of <%: versus <%= asp.net mvc tags: <%: %> vs. <%= %>

Variants of <% when writing code inside views in MVC2

I'm currently learning MVC2, and I have seen three variants of the tags that contain the actual code for a View:
<% ... %>
With a = after the %
<%= ... %>
and with a : after the %
<%: ... %>
What is the difference of these three code containers?
And are there any other variants of these?
<% ... %>
is just a block of code
<%: "blah blah" %>
Is Shorthand for
<%= Html.Encode("blah blah") %>
Which is shorthand for
<% Response.Write(Html.Encode("blah blah")) %>
1.<% ... %> just block of code
<%if (Model.HelloWorld != null){%>
Hello World!!
<%} %>
2.<%= ... %> plain text without escaping
<%=Model.HelloWorld %>
3.<%: ... %> text with escaping equal <%= Server.HtmlEncode(Model.Something) %> Details here
<%:Model.HelloWorld %>

Rails 3 Edit Multiple Records in a Single Form

I've been stuck on this problem for a couple of days now.
I've have some success with Railscasts Episode #198, but that one is for Rails 2. There have been some changes in Rails 3 that make it so the code provided in Episode #198 won't work.
The problem lies within the edit_individual.html.erb:
Original Code (provided by Ryan # Railscasts):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% fields_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
Modified Code (simply changed fields_for to form_for):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% form_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
In the new code, each record is placed within a form of their own, all inside one single form (which is the one I only want).
My question is, how can I get the code provided by Railscasts Episode #198 to work in Rails 3?
Here is a link to the Railscast I mentioned:
http://railscasts.com/episodes/198-edit-multiple-individually
Thank You,
c.allen.rosario
I found the solution. Just need to modify the following line in the code provided by Ryan # Railscasts:
<% fields_for "products[]", product do |f| %>
and change it to:
<%= fields_for "products[]", product do |f| %>
Notice, that the <% has been modified to <%=.
final solution:
<% form_tag update_individual_products_path :method => :put do %>
<% for product in #products %>
<%= fields_for "products[]", product do |f| %>
<h2><%= h product.name %></h2>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
I was wondering if anyone could explain this solution to me. From what I understand you should only need a <% in front of the fields_for.
c.allen.rosario
The change in Rails 3 from <% fields_for to <%= fields_for is because it was confusing that form_for, form_tag, etc... were using <% form... %> even though they they were outputting html code.
With Rails 3, since they output html code, they use <%=.
Please note that your first line is deprecated:
<% form_tag update_individual_products_path, :method => :put do %>
should be
<%= form_tag update_individual_products_path, :method => :put do %>
Same for all form tags.

rails 3 form_for doesn't output anything

Hi I have any form with nested form, for example
<% form_for :main do |f| %>
trying to insert code here
<% fields_for :nested do |nested_form| %>
<%= nested_form.text_field :description %>
<% end %>
<% end %>
And then I am trying to insert anything to a main form, nested form doesn't produce any output. It outputs only when it is the only object in main form. Any suggestions?
From the Rails 3 documentation examples you need to write your form_for like this:
<%= form_for :main do |f| %>
# trying to insert code here
<%= fields_for :nested do |nested_form| %>
<%= nested_form.text_field :description %>
<% end %>
<% end %>
Note the <%= for both form_for and fields_for

ASP.NET MVC2 TeplatedHelper doesn't render an ID of the HTML's markup

I have the code (snippet):
The Model is the IEnumerable object of the Person's class:
<% foreach (var item in Model)
{ %>
<tr>
<td><%= Html.DisplayFor(x=>item.Name) %></td>
</tr>
<% } %>
it renders only labels like that:
<td>Tommy</td>
According to the link it should be rendering a HTML markup something like:
but there is no the ID and the NAME property. Why ?
Your using the wrong template your should be using Html.EditorFor(x => x.Name)
Edit: I said you were using the wrong template because in your image it is a textbox displayed, not a label...
the default ouptut of Displayfor is
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %>
according to Brad Wilson. You could easily build your own, look the other post of Brad Wilson for examples.
Or you could simply call Html.LabelFor(x => x.Name)
If you always want that, add a template, name String.ascx in your Views/Share/DisplayTemplate and just put the following in :
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Label("", ViewData.TemplateInfo.FormattedModelValue) %>