HOW TO BIND TEXTBOX TO MODEL?

Gaurpriya Bishnoi

Total Post:7

Points:49
Posted by  Gaurpriya Bishnoi
Mvc4  Model 
 945  View(s)
Ratings:
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


  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: How to Bind textbox to Model?

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

  1. Gaurpriya Bishnoi

    Post:7

    Points:49
    Re: How to Bind textbox to Model?

    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

  1. Pawan Shukla

    Post:38

    Points:274
    Re: How to Bind textbox to Model?

    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!