How to pass values from One controller to another Controller in ASP.Net MVC3

asp.net-mvc-3, c#

Solution

You can try with Session, like

Session["username"] = username;

and for recover in the other controller use

var username = (string)Session["username"]

or in your redirect try with

return RedirectToAction("Index", "Nome", new{ username: username})

but the action of your controller must have as argument the (string username) like

public ActionResult Index(string username)
{
    return View();
}

Problem

Hello In my project I have to pass a welcome message with username to the Index Page Its a MVC3 ASP.Net Razor project There are two controllers are there; One is Login Controller and the second one is Home Controller. From Login Controller, I have to pass UserName of the Login Person to the view Page. Login Controller redirect to Another controller called Home Controller .From there I have to pass that value to the view page. That's my issue. I have tried with single controller to view, its working. I cant use the single controller because Login Controller uses Login Page and Home Controller uses Home Page. Both are separate views. I have tried Like this, but its not working. Can you suggest a good Method to follow? Login Controller ``` public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(LoginModel model) { if (ModelState.IsValid) { if (DataAccess.DAL.UserIsValid(model.UserName, model.Password)) { FormsAuthentication.SetAuthCookie(model.UserName, false); return RedirectToAction("Index", "Home" ); } else { ModelState.AddModelError("", "Invalid Username or Password"); } } return View(); } ``` Home Controller ``` public ActionResult Index() { return View(); } ```

Original source

Related problems