-1

I want to use @Html.DisplayFor instead of @Html.TextBoxFor in my application.

If I use @Html.TextBoxFor then I can pass values to controller but if I use @Html.DisplayFor then it is passing null everytime.

Please suggest me how to use the same data which I am passing in Textbox should pass into DisplayFor.

    @for (int counter = 0; counter <= Model.Employees.Count - 1; counter++)
        {
         <tr>
           <td>
          @Html.TextBoxFor(m => m.Employees[counter].Name, null, new { @class = "form-control" }) do not want to use

          @Html.DisplayFor(m => m.Employees[counter].Name, null, new { @class = "form-control" })   ///want to use
                </td>
    }
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
MVC
  • 649
  • 7
  • 23
  • Possible duplicate of [Using a DisplayTemplate (with DisplayFor) for each item in a collection](https://stackoverflow.com/questions/5651697/using-a-displaytemplate-with-displayfor-for-each-item-in-a-collection) – Erik Philips Jul 04 '18 at 02:21

1 Answers1

0

If you want values to be included in the form submission, they have to be bound to a form input. This is true of any form submission, not just ASP.NET MVC.

To include values that are not editable, you would use

@Html.HiddenFor(m => m.YourPropertyHere)

which generates a <input type="hidden" /> element for that property.

So, your example would look something like this:

@for (int counter = 0; counter <= Model.Employees.Count - 1; counter++)
{
    <tr>
        <td>
            @Html.DisplayFor(m => m.Employees[counter].Name, null, new { @class = "form-control" })
            @Html.HiddenFor(m => m.Employees[counter].Name)
        </td>
    </tr>
}
Tieson T.
  • 20,774
  • 6
  • 77
  • 92