-2

I want to have a dynamic table in MVC. I copied some code from the internet but it doesn't work. I haven't an error but I get no values on my site. My goal is just a little website with a dynamic table.

View:

@model IEnumerable<ProjectName.Models.TestModel>
@using (Html.BeginForm())
{
    <table width="960px">
        <tr>
            @{
                int crow = 1;
                foreach (var item in Model)
                {
                    <td style="border: 1px solid black;" width="600px">
                        <ul style="list-style: none;">
                            <li>
                                @Html.TextBox("txt")
                            </li>

                        </ul>
                    </td>
                    if (crow % 3 == 0)
                    {
                    <tr>
                        <td style="width: 285px; height: 50px"></td>
                    </tr>
                    }
                    crow++;
                }
            }
            </tr>
        </table>
}

Model:

 public class TestModel
 {
      public string numbers { get; set; }
 }

Controller:

List<testModel> model = new List<testModel>();
        int[] numbersdata = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0, 15, 14, 11, 13, 19, 18, 16, 17, 12, 10 };

        var lowNums = from n in numbersdata where n > 5 select n;

        foreach (var x in lowNums)
        {
            model.Add(new testModel()
            {
                numbers = x.ToString()
            });
        }
        return View(model);
Seabizkit
  • 2,417
  • 2
  • 15
  • 32
Mister
  • 25
  • 6
  • 1
    What is not working. What are you expecting to happen and what actually happens (note that form control has no relationship to the model and wont bind to anything) –  Jan 19 '17 at 08:15
  • Is it shows "txt" text several times? you should change html.textbox row as @Muntajib said – kgzdev Jan 19 '17 at 08:20
  • Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) to understand how to bind to your collection –  Jan 19 '17 at 08:40
  • *"I copied some code from the internet but it doesn't work"* That's always a good way to state your problem ... – Manfred Radlwimmer Jan 19 '17 at 09:23
  • What should I write if I haven't any ideas what could be wrong. It doesn't work that's the point! – Mister Jan 19 '17 at 13:55

2 Answers2

0

Your are just declaring a text box but not setting the value to text box.

Insted of this

@Html.TextBox("txt")

Use this:

 @Html.TextBox("txt",item)
Muntajib
  • 141
  • 5
0

replace @Html.TextBox("txt") with @item.numbers

Seabizkit
  • 2,417
  • 2
  • 15
  • 32