Mike Larche
Mike Larche

Reputation: 1

add a link a in controller instead of in a view

I know it's unusual to have a link a controller but in my case I have to have it there. here is my code: Controller:

    switch (Auth.AuthenticationResult)
                        {
                            case AuthenticationResult.Failed:
                                Error = "Incorrect password. Click here if you forgot your credentials";
    ModelState.AddModelError("", Error);

I would like to have the Click Here as a link to another controller.

How would I do that?

Upvotes: 0

Views: 52

Answers (2)

user4103496
user4103496

Reputation:

One option is to RedirectToAction() inside your switch, assuming the switch is in a controller class. The action that you redirect to would be associated with a view which has the link to wherever it is you want your link to go to.

This would take the form of:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        switch (Auth.AuthenticationResult)
                    {
                        case AuthenticationResult.Failed:
                            return RedirectToAction("NewView", "ErrorController");
                    }
        return View();
    }
}

In this scenario you send the user to the action "NewView" located in the controller "ErrorController". Your "NewView" view, can then be setup to handle this particular error with a custom message and link for the user to proceed elsewhere.

More information on RedirectToAction here.

Upvotes: 0

Paul Abbott
Paul Abbott

Reputation: 7211

Please explain the "have to have it there". In your controller

ViewBag.AuthenticationResult = Auth.AuthenticationResult

then in the view

@if (ViewBag.AuthenticationResult == AuthenticationResult.Failed)
{
    <span>Incorrect password. </span>
    @Html.ActionLink("Click here", "ActionName", "ControllerName")
    <span>if you forgot your credentials</span>
}

Upvotes: 1

Related Questions