开发者

simple question: Difficulty in declaring and using variable

开发者 https://www.devze.com 2023-01-03 11:31 出处:网络
What is wrong with the following code:I`m having the error message Error 1; expected <%if (Model.ReferenceFields != null)

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>
            <%}
               %>
         <%}
        %>


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>
        <%
    }
}
%>
0

精彩评论

暂无评论...
验证码 换一张
取 消