Sibeal
Sibeal

Reputation: 65

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")

In this case, the link is /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

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

@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>
}

Upvotes: 2

Views: 1976

Answers (2)

Totodile
Totodile

Reputation: 150

Just delete [HttpPost] it says that this controller only can be access by POST method an you are trying to access by GET, you should let it post and do the call with AJAx

Upvotes: 1

Joel R Michaliszen
Joel R Michaliszen

Reputation: 4222

It's because href attr in a a element just do GET dont POST, so to it works change the Action to :

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

Upvotes: 2

Related Questions