0

I am arranging a dynamic table of textboxes using a loop. Please review the following code segment in view -

<% foreach (var item in Model.Names) { %>
<% =Html.TextBoxFor(item.Name)%>
<% } %>

The problem here is that i want their unique ids to be generated so that i could access each textbox using JS.

Any help is highly appreciated.

Regards Irfan

1 Answer 1

1

Do this instead:

<div class="editor-field">
<% for (int i = 0; i < Model.Names.Count; i++) { %>
    <%: Html.TextBoxFor(m => m.Names[i].Name) %>
<% } %>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Martin, but will it generate unique ids for each textbox?
Yes, the ids will look like this: Names_0_Name, Names_1_Name, Names_2_Name and so on. And it will automatically bind back to your collection, when posting the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.