[英]passing string to controller/action from view
I am trying to lower the role of a user by clicking a link in my view. 我试图通过单击视图中的链接来降低用户的角色。
When I click the link, it doesn't go to the action, but it just gives 404 error and links the resource that is not found with the string I am trying to pass to the action( referred to as "stringparameter") 当我单击链接时,它不会转到操作,但只会显示404错误,并将找不到的资源与我尝试传递给操作的字符串链接(称为“ stringparameter”)
In this case, the link is /Admin/Disable/stringparameter 在这种情况下,链接为/ Admin / Disable / stringparameter
I think I am not using the correct overload, so could someone help me out? 我认为我没有使用正确的超载,所以有人可以帮助我吗? Thanks 谢谢
This is the action in the AdminController 这是AdminController中的操作
[HttpPost]
public ActionResult Disable(string id)
{
Role rol = new UserRepository().GetRole("Disabled");
new UserRepository().UpdateUser(id,rol.RoleId);
return RedirectToAction("Users");
}
this is the viewmodel 这是视图模型
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; }
}
this is the userclass 这是用户类
public class UserClass
{
public string UserId { get; set; }
public string Username { get; set; }
public string Role { get; set; }
}
and this is the view(1 of the 4 similar tables in the view) 这是视图(视图中4个相似表中的1个)
@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/Disable?id=@item.UserId">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>
}
It's because href
attr in a a
element just do GET
dont POST
, so to it works change the Action to : 这是因为href
在ATTR a
元素只是不GET
不POST
,所以它的工作原理改变行动:
[HttpGet]
public ActionResult Disable(string id)
{
Role rol = new UserRepository().GetRole("Disabled");
new UserRepository().UpdateUser(id,rol.RoleId);
return RedirectToAction("Users");
}
But I suggest to you do this with JS. 但我建议您使用JS执行此操作。
只需删除[HttpPost],它说该控制器只能通过POST方法访问,而您正尝试通过GET访问,则应让它发布并使用AJAx进行调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.