2015-12-30 78 views
2

我试图通过单击我的视图中的链接来降低用户的角色。将字符串传递给控制器​​/查看动作

当我点击链接时,它没有进入动作,但它只是给出了404错误,并且链接了未找到的资源和我试图传递给动作的字符串(称为“stringparameter “)

在这种情况下,链接/管理/禁止/ stringparameter

我想我没有使用正确的过载,因此可能有人帮助我吗? 由于

这是AdminController

[HttpPost] 
    public ActionResult Disable(string id) 
    { 
     Role rol = new UserRepository().GetRole("Disabled");    
     new UserRepository().UpdateUser(id,rol.RoleId); 
     return RedirectToAction("Users"); 
    } 

这个动作是视图模型

public class UserSuperUserPM 
{ 
    public UserClass User { get; set; } 
    public List<UserClass> Users { get; set; } 

    public UserClass SuperUser { get; set; } 
    public List<UserClass> SuperUsers { get; set; } 

    public UserClass Disabled { get; set; } 
    public List<UserClass> Disableds { get; set; } 

    public UserClass Inactive { get; set; } 
    public List<UserClass> Inactives { get; set; } 
} 

这是UserClass的

public class UserClass 
{ 
    public string UserId { get; set; } 
    public string Username { get; set; } 
    public string Role { get; set; } 
} 

,这是图(1在视图中的4个类似的桌子)

@foreach (var item in Model.Users) 
{ 
    <tr> 
     <td class="col-md-4"> 
      @Html.DisplayFor(modelItem => item.Username, Model.Users) 
     </td> 
     <td class="col-md-4"> 
      @Html.DisplayFor(modelItem => item.Role, Model.Users) 
     </td> 
     <td calss="col-md-4"> 
--------Commented attempted links(none of them work correct) 
      @*@Html.ActionLink("Disable", "Disable", new { Controller = "Admin", action = "Disable", id = item.UserId })*@ 
      @*<a href="~/Controllers/AdminController/[email protected]">Disable</a>*@ 
      @*@Html.ActionLink("Disable","Admin", new { id = item.UserId },null)*@ 
      @*@Html.ActionLink("Disable", "Disable","Admin", item.UserId)*@ 

-------original attempted link 
      @Html.ActionLink("Disable", "Disable", new { id = item.UserId}) 




     </td> 
    </tr> 
} 

+1

你的控制器的呼叫接入要求POST操作,不是GET。 –

回答

2

这是因为href ATTR在a元素只是GETPOST,所以它的工作原理改变行动:

[HttpGet] 
public ActionResult Disable(string id) 
{ 
    Role rol = new UserRepository().GetRole("Disabled");    
    new UserRepository().UpdateUser(id,rol.RoleId); 
    return RedirectToAction("Users"); 
} 

不过我建议你这样做与JS。

+0

哦傻了,忘了它需要GET,谢谢指出! 在课程中我们学会了这样做,所以为了使用我的老师的方法,因为我知道他们,我会继续像这样使用它,也许我可以在未来的项目上尝试JS,谢谢你的提示^^ – Sibeal

1

只要删除[HttpPost]它说,只有这个控制器可以是通过POST方法的您试图访问的GET,你应该让他发布,并做AJAX

+0

哦,傻我忘记它需要得到,感谢指出! – Sibeal

+0

对你的牛感到羞愧。 np XD – Totodile

相关问题