Scottish Smile
Scottish Smile

Reputation: 1121

ASP.Net MVC - How can a button access url query string parameters?

I'm working on a password reset project.

Users are sent an email with a url like: mysite.com/ResetPass?id=10&token=233rgths567sdsfg

This contains a user Id and JWT token in the url as parameters.

The MVC controller shows the password reset page.

        [HttpGet("ResetPass")]

        public IActionResult ResetPass([FromQuery] int id, [FromQuery] string token)
        {
            // How to send the id and token from the Url query string to the Button???

            return View("ResetPass");
        }


        // Reset Password Button
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> ResetPassButton()
        {
             // How to access the user id and token?
        }

I want to have a form on this page that allows users to change their password then send the new password, user Id and token to the API for processing.

But how do I get the variables from the url (the user id and token) to the button?

Do I just use Viewbag and pass them to the page? Or is there a way for the button to just access the Url query string?

Upvotes: 0

Views: 550

Answers (1)

Adlorem
Adlorem

Reputation: 1507

You can create reset password model and pass this model around. Example:

public class ResetPasswordModel
{
    public int Id { set; get; }
    public string Token { set; get; }
}

Then:

    public IActionResult ResetPass([FromQuery] int id, [FromQuery] string token)
    {
        var model = new ResetPasswordModel {
           Id = id,
           Token = token,
        }

        return View("ResetPass", model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> ResetPassButton(ResetPasswordModel model)
    {
         // do something with model
    }

Upvotes: 1

Related Questions