0

I have a view that shows users in the system, including user's status. I need to add a button for each record, this button should pass the user ID for a function in the controller, and this function should've change the value of user's status so if the status is true then it should be changed to false.

The problem here is the button I've added is not calling the function. I need to know why ...

Controller

    [HttpPost]
    public async Task<IActionResult> Change(string userid)
    {
        var user = await _userManager.FindByIdAsync(userid);

        user.Status = !user.Status;

        await _userManager.UpdateAsync(user);

        return View();
    }

View

<input type="button" asp-controller="Users" asp-action="Change" 
       asp-route-userid="@user.Id" value="Activate/De Activate" />

Many thanks

4
  • 2
    A button and a click on a button triggers a GET call - not a POST - but your controller's method is annotated with the [HttpPost] , so it will only respond to POST requests..... Commented Dec 24, 2022 at 14:30
  • Ok nice, I just figure out that i don't need a post method because i am not sending data back to the controller i just wanna trigger the function using the button so it can change the statues... but even i tried to remove function method in the controller so it's turned to a GET method, it's still not working.. you have any idea?? Commented Dec 24, 2022 at 14:59
  • 1
    Hi , Please Share Screenshots of your Browser Console and Your Network tab's XHR tab , this will provide more details and help us to figure out that whether there is Console error available or not, and there is a Console error than what it is.thanks Commented Dec 25, 2022 at 17:32
  • I replaced the button with this tag <a></a>, and i added the tag helpers asp-action and asp-controller, and then i removed the post method type from the controller.. it's worked. Commented Dec 26, 2022 at 1:24

1 Answer 1

1

I replaced the button with this tag , and i added the tag helpers asp-action and asp-controller, and then i removed the post method type from the controller.. it's worked

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.