c# - ASP.NET MVC 4 - for循环中的 Razor 空帖子

标签 c# asp.net-mvc razor

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

当我将数据发布到 Controller 时,它变为空。 我已经看到一个问题,它说你不能使用 foreach 循环 here . 在网站本身上,它确实显示了所有数据,但不会将其提供给 Controller 。

Controller :

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

型号:

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

最佳答案

您的代码需要在表单内,按钮类型“提交”将不起作用。您需要使用输入类型“提交”。见下面的代码:

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

关于c# - ASP.NET MVC 4 - for循环中的 Razor 空帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47353192/

相关文章:

ASP.NET Core - TagHelper、部分 View 和属性名称冲突

c# - 尝试让 View 在 MVC 中使用 View 模型

c# - 从 Excel 中读取列,重新格式化单元格

c# - 在插入命令asp中获取ID并在c#后面的代码中使用它

c# - 使用 IStorage/IStream 从 C# 拖放到 Windows 资源管理器

javascript - 页面加载时,@Url.Action 为 null 或 javascript 中的 ""

asp.net - 新域名的 URL 重写

asp.net-mvc - MVC CheckBoxFor 导致绕过不引人注目的验证

javascript - 如何根据文档日期在财政年度中设置日期选择器范围?我使用 JQUERY 来做到这一点

c# - LINQ 中的展平列表