Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple question: Difficulty in declaring and using variable

Tags:

asp.net-mvc

What is wrong with the following code: I`m having the 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>
            <%}
               %>
         <%}
        %>
like image 259
learning Avatar asked Aug 19 '26 07:08

learning


1 Answers

The basic problem is lines like this

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

So you're wanting to write out the contents of value into the html but thats not the way to do it. It should be

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

or a shortcut for Response.Write is <%= so

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

ASP101 - Writing Your First ASP.NET Page

The other problem is that the formatting of your code is, quite frankly butt ugly and you're making it hard work for yourself when 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>
        <%
    }
}
%>
like image 192
Ryan Avatar answered Aug 20 '26 22:08

Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!