1

I have the following code in razor:

  @for (int i = 0; i < Model.Count; i++)
  {
      using (Html.BeginForm("Bonnen", "Bon")) 
      {
          <tr>
              <td> @Html.TextBoxFor(model => Model[i].Id)</td>
              <td>@Html.DisplayFor(model => Model[i].Date)</td>
              <td>@Html.DisplayFor(model => Model[i].Description)</td>
              <td><button type="submit" value="Details" class="btn btn-info">Details</button></td>
          </tr>
      }
  }

When i post the data to the controller it becomes empty. I've already seen a question where it said you can't use a foreach loop here. On the website itself it does show all the data, but it won't give it to the controller.

Controller:

  [HttpPost]
  public ActionResult Bonnen(Bon bon)
  {
   return RedirectToAction("Details", "Bon", bon);
  }

Model:

  public int Id { get; set; }
  public string Description { get; set; }
  public DateTime Date { get; set; }

4 Answers4

3

Your code needs to be inside the form and button type "submit" wont work. You need to use input type "submit". See code below:

  @using (Html.BeginForm("Bonnen", "Bon")) 
  {
    @for (int i = 0; i < Model.Count; i++)
    {
       <tr>
           <td> @Html.TextBoxFor(model => Model[i].Id)</td>
           <td>@Html.DisplayFor(model => Model[i].Date)</td>
           <td>@Html.DisplayFor(model => Model[i].Description)</td>
           <td><input type="submit" value="Details" class="btn btn-info"/></td>
       </tr>
    }
  }
Heimi
  • 124
  • 1
  • 12
1
 @using (Html.BeginForm("Bonnen", "Bon",FormMethod.Post)) 
  {
    @foreach (var item in Model)
    {
       <tr>
           <td> @Html.TextBoxFor(model => item.Id)</td>
           <td>@Html.DisplayFor(model =>  item.Date)</td>
           <td>@Html.DisplayFor(model =>  item.Description)</td>
       </tr>
    }
    <input type="submit" value="Details" class="btn btn-info"/>
  }

[HttpPost]
  public ActionResult Bonnen(int[] Id ,DateTime[] Date,string[] Description)
  {
   return RedirectToAction("Details", "Bon", bon);
  }
Farhad Bagherlo
  • 6,389
  • 3
  • 19
  • 44
0

Try the following code

@for (int i = 0; i < Model.Count; i++)
{
  using (Html.BeginForm("Bonnen", "Bon")) 
  {
      <tr>
          <td> @Html.TextBox("Id", Model[i].Id)</td>
          <td>@Html.Display("Date", Model[i].Date)</td>
          <td>@Html.Display("Description", Model[i].Description)</td>
          <td><button type="submit" value="Details" class="btn btn-info">Details</button></td>
      </tr>
  }
}

If you want the values of Date and Description too in controller then change them from Display to Editor.

Nitesh Kumar
  • 1,741
  • 4
  • 20
  • 26
0

managed to solve it with a foreach loop.

@using (Html.BeginForm())
{
 <h2>Bon</h2>
 <input class="form-control" id="myInput" type="text" placeholder="Zoek..." />
 <br />
 <table style="width: 100%">
  <thead>
   <tr>
    <th>Id</th>
    <th>Datum</th>
    <th>Boodschappen</th>
   </tr>
  </thead>
  <tbody>
   @foreach (var item in Model)
   {
   <tr>
    <td>
     @Html.DisplayFor(modelItem => item.Id)
    </td>
    <td>
     @Html.DisplayFor(modelItem => item.Date)
    </td>
    <td>
     @Html.DisplayFor(modelItem => item.Description)
    </td>
    <td>
     @using (Html.BeginForm("Bon", "Bon", FormMethod.Post))
    {
     @Html.AntiForgeryToken()
     @Html.ActionLink("Details", "Details", "Bon", new { id = item.Id }, null)
    }
    </td>
   </tr>
   }
  </tbody>
 </table>
}