A simple question: the difficulty of declaring and using a variable

What's wrong with the following code: I have an error message

Error 1; expected

    <%if (Model.ReferenceFields != null)
          {%>
            <%int count = 1; %>

            <%foreach (var referenceName in Model.ReferenceFields)
            {%>
             <%var value = "value"; %>
             <%count++; %>
             <%value = value + count.ToString(); %>
            <tr>
                <td><input type="hidden" name="Tests.Index" value='<%value%>' /></td>
                <td><input type="text" name="Tests['<%value%>'].Value"/></td>
                <td><input type="button" value= "Add" /></td></tr>
            <%}
               %>
         <%}
        %>
+3
source share
1 answer

The main problem is lines like this

<input type="hidden" name="Tests.Index" value='<%value%>' />

So you want to write the contents of the value in html, but that is not the way to do it. It should be

<input type="hidden" name="Tests.Index" value='<% Response.Write(value); %>' />

or shortcut for Response.Write is <% = so

<input type="hidden" name="Tests.Index" value='<%= value %>' />

ASP101 - Writing the First Page of ASP.NET

Another problem is that formatting your code, quite frankly with the butt, is ugly, and you are trying to work hard, trying to read it. Try this instead.

<%
if (Model.ReferenceFields != null)
{
    int count = 1; 
    foreach (var referenceName in Model.ReferenceFields)
    {
        var value = "value";
        count++;
        value = value + count.ToString(); 
        %>
        <tr>
        <td><input type="hidden" name="Tests.Index" value='<%= value %>' /></td>
        <td><input type="text" name="Tests['<%= value %>'].Value"/></td>
        <td><input type="button" value= "Add" /></td></tr>
        <%
    }
}
%>
+2
source

Source: https://habr.com/ru/post/1750281/


All Articles