-3

I am doing an .NET MVC project, there i am running a for loop to create n number of textboxes and checkboxes, I want to fill data in textbox when corresponding checkbox is checked. Since all the textboxes and the checkboxes have same ID and Name, I am facing problem.

foreach (var item in Model)
{                   
    <input type="checkbox" name="BooksToCart" id="BooksToCart" value="@item.BookId">
    <input type="number" name="NoBooksToCart" id="NoBooksToCart">
}

I would also like the javascript code. Thanks!

Liviu Boboia
  • 1,734
  • 1
  • 10
  • 21
Praveen Mohan
  • 211
  • 1
  • 6
  • 16
  • That doesn't look like valid razor code to me. – Sam Axe Mar 16 '17 at 07:26
  • Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) for how to correctly generate form controls for a collection –  Mar 16 '17 at 07:28
  • "Since all the textboxes and the checkboxes have same ID and Name, I am facing problem". Yes you are, Id's **must** be unique. Are you open to using jquery? – Jon P Mar 16 '17 at 07:49

1 Answers1

1

You can make different id of text box and check box as below

foreach (var item in Model)
{
  <input type="checkbox" name="BooksToCart" id="BooksToCart-@item.BookId"    value="@item.BookId">
  <input type="number" name="NoBooksToCart" id="NoBooksToCart-@item.BookId">
}
Vindhyachal Kumar
  • 1,713
  • 2
  • 23
  • 27