4

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();
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Nidheesh
  • 426
  • 4
  • 12
  • 25

4 Answers4

19

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();
}
theLaw
  • 1,261
  • 2
  • 11
  • 23
  • Passing data as parameter to RedirecttoAction doesn't work. The reason for this has been explained in this answer - https://stackoverflow.com/a/32174158/5333178 – Abhishek Poojary Sep 06 '17 at 11:43
  • According to MSDN and to what i made it works https://msdn.microsoft.com/it-it/library/system.web.mvc.controller.redirecttoaction(v=vs.118).aspx – theLaw Sep 06 '17 at 14:03
4

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

[Authorize]
public ActionResult Index()
{
    string username = User.Identity.Name;
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3
  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. :)

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
Soumya
  • 43
  • 1
  • 8
2

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

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

// home/index
var message = TempData["message"] as string;
Mike Koder
  • 1,898
  • 1
  • 17
  • 27