1

Can I pass table td values to controller?

View strongly typed:

@using (Html.BeginForm("PostClick", "Vendor", FormMethod.Post)) {
<table class="tblData">
  <tr>
    <th>
      @Html.DisplayNameFor(model => model.First().SubmittedDate)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.First().StartDate)
    </th>
  </tr>

  <tr>
    <td>
      @Html.DisplayFor(modelItem => item.SubmittedDate)
    </td>
    <td>
      @Html.DisplayFor(modelItem => item.StartDate)
    </td>
   </tr>
 </table>
 <input type="submit" value="submit" />
    }

Contoller code:

public void PostClick(FormCollection collection)
{
   /*Some Code */
} 

How to pass table value from view to controller?

Have used JasonData & Ajax call and able to send the table data to controller.

Want to know any other method can be done because FormCollection data not able to find table values

tereško
  • 58,060
  • 25
  • 98
  • 150
Vinod
  • 596
  • 2
  • 10
  • 28
  • 1
    `@Html.DisplayFor()` does not create controls that post back. You need to generate `input`, `textarea` or `select` elements - e.g. using `@Html.TextBoxFor()`. And the fact you are using `model.First().SubmittedDate` suggests you have a collection which means you need to generate each table row in a `for` loop. And you should be making use of MVC's model binding features by posting back your model (not using `FormCollection`) –  Jan 12 '15 at 06:10
  • thanks Stephen. Yes replaced FormCollection with EnityClass public void PostClick(List Vendor) { } . But List Vendor object is showing null. What can be replaced with DisplayFor() for the above Context to achieve result in Vendor list. – Vinod Jan 12 '15 at 06:18
  • use ``Vender`` not ``List`` it is a single object not collection – Ehsan Sajjad Jan 12 '15 at 06:37
  • Is your model in the view `@model List`? In which case you need `for(int i = 0; i < Model.Count; i++){ @Html.TextBoxFor(m => m[i].SubmittedDate) ..... }` –  Jan 12 '15 at 06:41
  • Thanks All for reply. Yes have used for loop . Data is displaying correctly at View . Have to Pass Table Value from View to Controller. that is actual Issue now ???? – Vinod Jan 12 '15 at 06:49

1 Answers1

5

Your need to generate controls that post back (input, textarea or select) and generate those controls in a for loop (or use a custom EditorTemplate for type Vendor)

View

@model List<Vendor>
@using (Html.BeginForm())
{
  <table class="tblData">
    <thead>
      ....
    </thead>
    <tbody>
      for(int i = 0; i < Model.Count; i++)
      {
        <tr>
          <td>@Html.TextBoxFor(m => m[i].SubmittedDate)</td>
          <td>@Html.TextBoxFor(m => m[i].StartDate)</td>
        </tr>
      }
    </tbody>
  </table>
  <input type="submit" value="submit" />
}

Post method

public void PostClick(List<Vendor> model)
{
  /*Some Code */
} 
  • 1
    @M.FawadSurosh, If you are having a problem, ask a new question with the relevant code (I cannot guess what mistakes you have made) –  Jul 25 '18 at 22:15