Mike Diaz
Mike Diaz

Reputation: 2065

MVC 3/4 Pass Parameter from Controller to Another Controller

I have a Login form, It asks for UserName, Password, TokenID (Each user knows his token, they get it to access an API). I would like to be able to Obtain that Token API that they entered and Pass it on to /Home/Index. What is the best way to go about doing that?

Upvotes: 0

Views: 971

Answers (1)

Yasser Shaikh
Yasser Shaikh

Reputation: 47774

Ok, first you have a View where user will be entering UserName, Password and TokenId so let the view have a model like the one shown below.

public class LoginModel{

public string UserName{get; set;} 
public string Password{get; set;}
public string TokenId{get; set;}
}

and from the view you can return this model, which will hold all these values.

Now as you need to pass these values to Home/Index you can have your form submitted to the Index action.

public ActionResult Index(LoginModel model)
{
 string gotcha = model.TokenId;
}

But incase if you want to submit a form to a different action of say a controller 'Login' and then pass this values to a different controller, you can do something like this.

// defined in Login Controller
public ActionResult Index(LoginModel model)
{
 TempData["TokenId"] = model.TokenId;
 return RedirectToAction("Dashboard","Account");
}

// defined in Account Controller
public ActionResult Dashboard()
{
 string gotVal = TempData["TokenId"]

}

there are more ways to pass values between two action, see this link

Upvotes: 1

Related Questions