Tripping
Tripping

Reputation: 919

for loop in razor to create list of input tags

I am new to MVC3 and web development in general.

I am trying to a list of input tags in razor view using the code below:

    @{
        for (Int32 i = 1; i < 10; i++)
        {
            <div class="upload-box" id="upload-box-@i" style="display:block">
                <div class="preview-box">
                    <img alt="Field for image cutting" id="preview-@i" src="@Url.Content("~/Content/empty.png")" />
                </div>
                <div id="filename-box-@i"></div>
                <div>
                    <label for="File[i]">Filename:</label>
                    <input type="file" name="Files" id="File@i" />
                </div>
            </div>
        }
    }

My question is related to

<label for="File[i]">Filename:</label>

and

<input type="file" name="Files" id="File@i" />

I am trying two version here to produce id of File1, File2 etc. None of them work.

I am not sure if this can be done? I have tried various alternatives and searched google - which makes me believe that the answer to my question is NO. But I thought I will check SO anyway.

Upvotes: 2

Views: 714

Answers (1)

Brandon
Brandon

Reputation: 69973

Razor isn't parsing the File@i correctly.

You can make it explicit by wrapping it in brackets.

<input type="file" name="Files" id="File@(i)" />

Also, the label doesn't have the @ sign.

Change it to

<label for="File[@i]">Filename:</label>

The brackets aren't required for this one, but you can add it anyways.

Upvotes: 4

Related Questions