2015-12-30 75 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

相關問題