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

4 Answers 4

20

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Passing data as parameter to RedirecttoAction doesn't work. The reason for this has been explained in this answer - stackoverflow.com/a/32174158/5333178
According to MSDN and to what i made it works msdn.microsoft.com/it-it/library/…
4

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

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

Comments

3

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

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

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

1 Comment

It's worth noting that this value is lost if the user refreshes the page.
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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.