简体   繁体   English

从视图将字符串传递给控制器​​/动作

[英]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元素只是不GETPOST ,所以它的工作原理改变行动:

[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.

相关问题 将视图模型从剃刀视图传递到控制器动作 - Passing view model from razor view to controller action 将动态生成的字符串从视图传递到控制器 - Passing a dynamically generated string from a View to a Controller 将下拉字符串从视图传递到控制器方法 - Passing a DropDown string from a View to a Controller method 将大型HTML字符串从View传递给Controller - Passing a large HTML string from View to Controller 发布操作无效,需要帮助,将信息从视图传递到控制器 - Post action is not working, need help passing infromation from view into the controller 将操作和控制器值从视图传递到另一个部分视图 - Passing action and controller values from view to another partialview 将Guid从视图传递回相应的控制器动作 - passing a Guid from a view back to corresponding controller action MVC控制器帮助:将字符串从视图传递到控制器 - Help with MVC controller: passing a string from view to controller 从一个视图路由到另一个控制器的操作方法,并将参数传递给该控制器的构造函数 - Routing from one view to another controller's action method and passing a parameter to that controller's constructor 将变量从视图传递到 controller 而不使用查询字符串 - Passing variable from view to the controller without query string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM