0

This is about an MVC WebApp (that also uses EF). My question is about returning to the same view after an asp-action.

I display a list of users (db records on my .cshtml page). Next to each user, I have a Reset Password hyperlink. Upon clicking the hyperlink, I use EF to reset the user's password. This is done through an asp-action

  <a asp-action="ResetPassword" asp-route-userid="@item.userid">Reset Password</a>

This reset asp-action is all happening fine.

But post asp-action, I would like to return back to the same view (from where I originally clicked the Reset Password hyperlink).

I do not need any refreshes or re-launch of the view. I find examples in StackOverflow about using Ajax if its a form/submit button action. In my case there's no submit button action or form or another view being shown.

Any suggestions on how to return back to the same View?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NRB
  • 95
  • 1
  • 7

1 Answers1

1

In your controller ResetPassword method, you can redirect to the action that initially displayed your view. You don't have your controller or view names listed in your question, so I'll make some guesses here:

    public class UsersController : Controller
    {
        public ActionResult Index()
        {
            //get user data/setup model (or do it from the view via ajax)

            return View(model);
        }

        public ActionResult ResetPassword(int id)
        {
            //do reset password work

            //return redirect to the Index page that lists your user data
            return RedirectToAction("Index", "Users");
        }
    }

Now, while the above will work, it would be a better experience for the user (and lower server/db impact) if the call to ResetPassword was done via ajax, since a full page refresh wouldn't be needed.

G_P
  • 2,100
  • 3
  • 16
  • 18
  • Thank you. I shall try. Are there any ajax examples of your recommendation ? – NRB Jul 28 '20 at 20:29
  • If you are using jquery, you can look at examples like this one: https://stackoverflow.com/a/47673365/493557 That example is very similar to your scenario, except it doesn't pass any data in the ajax call. Another example that does pass data: https://stackoverflow.com/a/37478253/493557 Those should get you enough to know what to search for to get ajax calls working. However, you don't *need* to move to ajax, it depends on your requirements, and if that is just an admin page that won't be used all the time, going the simpler route (non-ajax) may be fine. – G_P Jul 28 '20 at 20:38
  • The non-Ajax method actually refreshes. How I found this? When I press the backbutton, it wouldn't go back and needs one more back button click. I presume the Ajax method (or the partial view) method will prevent this. – NRB Jul 28 '20 at 21:09