How to Bind textbox to Model?

foreach (var item in Model)
{
    <tr>
      <td class="well"> @Html.TextBoxFor(m => item.UserMaster.D1, new { @class = "form-control" })</td>
     <td class="well"> @Html.TextBoxFor(m => item.UserMaster.D2, new { @class = "form-control" })</td>
     <td class="well"> @Html.TextBoxFor(m => item.UserMaster.D3, new { @class = "form-control" })</td>
     <td class="well"> @Html.TextBoxFor(m => item.UserMaster.D4, new { @class = "form-control" })</td>
     <td class="well"> @Html.TextBoxFor(m => item.UserMaster.D5, new { @class = "form-control" })</td>
     <td class="well"> @Html.TextBoxFor(m => item.UserMaster.D6, new { @class = "form-control" })</td>
     <td class="well"> @Html.TextBoxFor(m => item.UserMaster.D7, new { @class = "form-control" })</td>
     <td class="well"> @Html.TextBoxFor(m => item.UserMaster.D8, new { @class = "form-control" })</td>
    </tr>
}

I am want to make these textbox inside for loop how to possible


Last updated:7/9/2015 12:54:19 AM

3 Answers

Mayank Tripathi
Mayank Tripathi

Hi Gaurpriya please explain this a bit more so that we can analyse the issue and implement it 

Gaurpriya Bishnoi
Gaurpriya Bishnoi

Hi Mayank 


i am want to simple these textbox make inside for loop

foreach (var item in Model)
{
   for(int i=0;i<15;i++)
   {
    <tr>
      <td class='well'> @Html.TextBoxFor(m => item.UserMaster.D1, new { @class = 'form-control' })</td>
    </tr>
  }
}

But i am face problem in model binding
In above code every textbox is bind same column name (D1).
But i am want to every textbox is bind different different column like(D1, D2, D3)...etc
please solve this problem as soon soon

Anonymous User
Anonymous User

Hi Gaurpriya
Step 1:
Create a class
   public class FieldList
    {
        public string Name { get; set; }
    }
Step 2:
Add Property in your model 
public IList<FieldList> UserMaster { get; set; }
Step 3:
Use this code in your razor page.
 @for (int a = 0; a <= 10; a++)
    {
        @Html.EditorFor(m => m.UserMaster[a].Name);
    }


Answer