Imagine a view that is designed to show a list of items of type Foo:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Foo>>" %>
Now the page displays the list of items of type Foo:
<table>
<tr>
<th>
Name
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.Name) %>
</td>
</tr>
<% } %>
</table>
In addition to display the list of items on the page I also need to execute a Javascript function Bar for each of the items. Here's my first attempt:
<% foreach (var item in Model) { %>
<script type="text/javascript">
$(document).ready(function() {
var name = "<%=item.Name %>";
Bar(name)
});
</script>
<% } %>
I get an error "Cannot resolve symbol item" against the line that begins "var name...".
Is this the correct way to achieve this? What's the correct syntax to use?