Home > DeveloperSection > Forums > How to Bind textbox to Model?
Gaurpriya Bishnoi
Gaurpriya Bishnoi

Total Post:7

Points:49
Posted on    July-07-2015 2:29 AM

 ASP.NET MVC Mvc4  Model 
Ratings:


 3 Reply(s)
 568  View(s)
Rate this:
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




Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    July-07-2015 8:19 AM

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

Gaurpriya Bishnoi
Gaurpriya Bishnoi

Total Post:7

Points:49
Posted on    July-07-2015 10:39 PM

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

Pawan Shukla

Total Post:37

Points:267
Posted on    July-09-2015 12:54 AM

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);
    }



Don't want to miss updates? Please click the below button!

Follow MindStick