Nidheesh
Nidheesh

Reputation: 426

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

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();
}

Upvotes: 4

Views: 82206

Answers (4)

Soumya
Soumya

Reputation: 53

  1. Change the Index() method of Home Controller to this:

    [HttpPost]
    
    public ActionResult Index(string username)
    {
         ViewBag.user=username; 
         return View();
    }
    
  2. Modify the Login Controller :

    if (DataAccess.DAL.UserIsValid(model.UserName, model.Password))
    {
        FormsAuthentication.SetAuthCookie(model.UserName, false); 
        return RedirectToAction("Index", "Home",new { username = model.Username } ); 
        //sending the parameter 'username'value to Index of Home Controller
    }
    

Go to the View Page of the Index method of Home Controller and add the following:

 <p>User is: @ViewBag.user</p>

And you're done. :)

Upvotes: 3

theLaw
theLaw

Reputation: 1291

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();
}

Upvotes: 19

Mike Koder
Mike Koder

Reputation: 1928

Use TempData. Its data is available in the next request also.

// after login
TempData["message"] = "whatever";

// home/index
var message = TempData["message"] as string;

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could retrieve the currently authenticated username from the User instance:

[Authorize]
public ActionResult Index()
{
    string username = User.Identity.Name;
    ...
}

Upvotes: 4

Related Questions