0

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; }
3
  • Are you generating multiple forms on purpose or is that a typo? Commented Nov 17, 2017 at 14:43
  • It' s on purpose. Commented Nov 17, 2017 at 21:32
  • You produce broken html if you put <form> inside <table>. See this: stackoverflow.com/a/5967613/809357 Commented Nov 21, 2017 at 14:55

4 Answers 4

2

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>
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.