控制器中的HttpPost方法不与HttpGet方法

时间:2016-09-01 17:07:20

标签: asp.net-core-mvc asp.net-core-1.0

我试图在每个动作之前插入一个断点,当我在HttpGet方法中的ClassId值取得需要删除的类的实际(正确)Id而HttpPost方法中的ClassId值与之匹配时,我陷入了困境。 HttpGet方法的ClassId值,HttpPost中ClassId的实际值返回null,因为我遇到异常错误,该对象具有null值。如果有人能够承认我如何消除这个错误,那将是非常好的。谢谢。下面分别是控制器的代码和相应的视图。

控制器代码

 [HttpGet]
    public IActionResult Delete(int ClassId)
    {
        return View(db.Class.Include(x => x.Student).First(c => c.ClassId == ClassId));
    }


   [ActionName("Delete")]
    [HttpPost]
    public IActionResult DeletePost(int ClassId)
    {
        var r = db.Class.First(a => a.ClassId == ClassId);
        var student = r.StudentId.ToString();
        db.Remove(r);
        db.SaveChanges();
        return RedirectToAction("Details", "Student", new { id = student });
    }

查看代码

 @model DemoProject.Models.Class

 @{
   ViewData["Title"] = "Delete Class";
  }

   <h3>Are you sure you want to delete this?</h3>
                <div>
                    <hr />
                    <dl class="dl-horizontal">
                        <dt>
                            @Html.DisplayNameFor(model => model.ClassName)
                        </dt>
                        <dd>
                            @Html.DisplayFor(model => model.ClassName)
                        </dd>
                        <dt>
                            @Html.DisplayNameFor(model => model.Title)
                        </dt>
                        <dd>
                            @Html.DisplayFor(model => model.Title)
                        </dd>
                        <dt>
                            @Html.DisplayNameFor(model => model.Text)
                        </dt>
                        <dd>
                            @Html.DisplayFor(model => model.Text)
                        </dd>

                    </dl>

                    <form asp-action="Delete">
                        <div class="form-actions no-color">
                            <input type="submit" value="Delete" class="btn btn-danger" /> 
                           <div class="row">
                               <a asp-action="Details" asp-controller="Student" asp-route-id="@Model.Student.StudentId">Back</a>
                           </div>
                        </div>
                    </form>
                </div>

1 个答案:

答案 0 :(得分:3)

您有两种选择:

  1. 正如@ stephen.vakil所说,在ClassId元素中为<form>属性添加隐藏字段。
  2. 使用隐藏字段查看

    @model ModelBinding.Models.Class
    
    @{
        ViewData["Title"] = "Delete Class";
    }
    
    <h3>Are you sure you want to delete this?</h3>
    <div>
      <hr/>
      <dl class="dl-horizontal">
        <dt>
          @Html.DisplayNameFor(model => model.ClassName)
        </dt>
        <dd>
          @Html.DisplayFor(model => model.ClassName)
        </dd>
        <dt>
          @Html.DisplayNameFor(model => model.Title)
        </dt>
        <dd>
          @Html.DisplayFor(model => model.Title)
        </dd>
        <dt>
          @Html.DisplayNameFor(model => model.Text)
        </dt>
        <dd>
          @Html.DisplayFor(model => model.Text)
        </dd>
    
      </dl>
    
      <form asp-action="Delete">
        <div class="form-actions no-color">
          @Html.HiddenFor(model => model.ClassId)
          <input type="submit" value="Delete" class="btn btn-danger"/>
          <div class="row">
            <a asp-action="Details" asp-controller="Student" asp-route-id="@Model.Student.StudentId">Back</a>
          </div>
        </div>
      </form>
    </div>
    
    1. asp-route-id="@Model.ClassId"添加到您的<form>代码,并将ClassId方法的DeletePost参数名称更改为id
    2. 使用asp-route-id查看

      @model ModelBinding.Models.Class
      
      @{
          ViewData["Title"] = "Delete Class";
      }
      
      <h3>Are you sure you want to delete this?</h3>
      <div>
        <hr/>
        <dl class="dl-horizontal">
          <dt>
            @Html.DisplayNameFor(model => model.ClassName)
          </dt>
          <dd>
            @Html.DisplayFor(model => model.ClassName)
          </dd>
          <dt>
            @Html.DisplayNameFor(model => model.Title)
          </dt>
          <dd>
            @Html.DisplayFor(model => model.Title)
          </dd>
          <dt>
            @Html.DisplayNameFor(model => model.Text)
          </dt>
          <dd>
            @Html.DisplayFor(model => model.Text)
          </dd>
      
        </dl>
      
        <form asp-action="Delete" asp-route-id="@Model.ClassId">
          <div class="form-actions no-color">
            <input type="submit" value="Delete" class="btn btn-danger"/>
            <div class="row">
              <a asp-action="Details" asp-controller="Student" asp-route-id="@Model.Student.StudentId">Back</a>
            </div>
          </div>
        </form>
      </div>
      

      具有重命名的操作参数的控制器

      [HttpGet]
      public IActionResult Delete(int classId)
      {
          return View(db.Class.Include(x => x.Student).First(c => c.ClassId == classId));
      }
      
      [ActionName("Delete")]
      [HttpPost]
      public IActionResult DeletePost(int id)
      {
          var r = db.Class.First(a => a.ClassId == id);
          var student = r.StudentId.ToString();
          db.Remove(r);
          db.SaveChanges();
          return RedirectToAction("Details", "Student", new { id = student });
      }
      

      除非您出于某种原因无法使用<hidden>字段,否则选项1是最佳选择。