【问题标题】:ASP.NET MVC 4 - razor empty post in for loopASP.NET MVC 4 - for 循环中的剃刀空帖子
【发布时间】:2017-11-17 14:41:08
【问题描述】:

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

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

控制器:

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

【问题讨论】:

  • 您是故意生成多个表单还是拼写错误?
  • 这是故意的。
  • 如果您将&lt;form&gt; 放入&lt;table&gt; 中,则会产生损坏的html。看到这个:stackoverflow.com/a/5967613/809357

标签: c# asp.net-mvc razor


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
     @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);
      }
    

    【讨论】:

      【解决方案3】:

      试试下面的代码

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

      如果您也希望控制器中的日期和描述的值,则将它们从显示更改为编辑器。

      【讨论】:

        【解决方案4】:

        设法通过 foreach 循环解决了这个问题。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-16
          • 1970-01-01
          • 2015-03-29
          • 2011-04-12
          • 2011-06-09
          • 2011-12-05
          相关资源
          最近更新 更多