Search code examples
c#asp.net-mvcmodel-view-controlleractionlink

react in controller, which and if action link is clicked in view


In my view "EditUser" I have an action link which i click not always:

  <%= Html.ActionLink("Resend forgotten password", "EditUser", this.Model.UserName, null)%><br />

In my controller "AdministrationController" i have an EditUser ActionResult there i would like to call Method which send forgotten password. But I dont know how to react if i clicked an action link or not. I dont want to send Password each time when I call Action "EditUser".

My Action in AdministratorController:

[HttpPost]
        [ValidateInput(false)]
        public ActionResult EditUser(EditUserModel model)
        {
        try
        {
            Admin admin = new Admin();
            admin.SendForgottenPasswordToUser(model.UserName);

            if (!model.Validate())
            {
                ModelState.AddModelError("", "Please fill missed fields");
            }

            if (!model.Save())
            {
                ModelState.AddModelError("", "Error while saving data");
            }
            else
            {
                ModelState.AddModelError("", "Successufully edited.");
            }

            return View(model);

        }

Solution

  • You could create an extra action method and change the action link:

    <%= Html.ActionLink("Resend forgotten password", "ResendPassword", this.Model.UserName, null)%>

    The action method for resending the password then looks something like this:

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult ResendPassword(EditUserModel model)
    {
        try
        {
            Admin admin = new Admin();
            admin.SendForgottenPasswordToUser(model.UserName);
            return View("EditUser", model);
        }
        catch (Exception ex)
        {
            // Error handling here.
        }
    }