2016-09-01 72 views
0

我試圖在每個操作之前插入一個斷點,並且我在HttpGet方法中的ClassId值取需要刪除的類的實際(正確)Id時卡住了一點HttpPost方法中的ClassId值與HttpGet方法的ClassId值不匹配,HttpPost中ClassId的Infact值返回null,因此我遇到了對象具有空值的異常錯誤。如果有人能夠承認我可以消除這種錯誤,那將是非常好的。謝謝。下面分別是控制器的代碼和相應的視圖。控制器中的HttpPost方法與HttpGet方法不協調

控制器代碼

[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

@CoreySmith我剛剛發佈了它 – Priyanshu

+1

你似乎沒有將ID從你的表單傳遞給post方法。使用隱藏字段或類似內容。 –

回答

3

你有兩個選擇:

  1. 正如@ stephen.vakil說,添加一個隱藏字段的ClassId屬性您在<form>元素內的視圖。

查看與隱藏字段

@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> 
  • 添加asp-route-id="@Model.ClassId"到您<form>標籤和更改DeletePost方法的ClassId參數名id
  • 查看與ASP路由-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是最好的選擇。