2015-07-02 88 views
0

我在表格中有一个Delete按钮,我尝试删除所选的Row。将Id传递给ActionMethod

的问题是,我总是在后的方法得到一个空ID

<div> 
<table class="table"> 
    <thead> 
     <tr> 
      <th>Role Name</th> 
      <th>Action</th> 
     </tr> 
    </thead> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.Name) 
      </td> 
      <td> 
       @using (Html.BeginForm("Delete","Role", new { id = item.Id },FormMethod.Post)) 
       { 
        <div class="form-actions no-color"> 
         <input type="submit" value="Delete" class="btn btn-default" /> | 
        </div> 
       } 
      </td> 
     </tr> 
    } 

</table> 

在我的控制器

// POST: Jobs/Delete/5 
     [HttpPost, ActionName("Delete")] 
     public ActionResult Delete(int id) 
     { 
      IdentityRole role = _context.Roles.Find(id); 
      _context.Roles.Remove(role); 
      _context.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

任何时候,我点击按钮的id为null

+0

向我们显示您的路线? – ramiramilu

+0

'location.href'是一个GET。你不应该试图对'Delete()'方法进行GET调用。而且你的方法在任何情况下都用'[ValidateAntiForgeryToken]'来装饰(但你不传递该标记)。添加一个表单并在其中添加一个提交按钮(并且包含'@ Html.AntiForgeryToken()' –

+0

@StephenMuecke我编辑了我的代码并添加了表单,但我仍然得到一个空ID –

回答

2

从您的意见中,生成在<form>标记中的html是

action="/Role/Delete/b1bc13ca-a855-48b0-90e2-9e5fc081ac86" 

这意味着你的模型的Id属性的typeof Guid。将POST方法签名更改为

public ActionResult Delete(Guid id)