2

It is easy to find out in the view but is there any way to get users Name in the index controller?

I have my index action and based on whether user loged in I want the login to be passed a sparameter to my index action like so:

 public ActionResult Index(userName)
    {

        var topTenList = repository.FindAllTopTen(userName).ToList();
        var currentList = repository.FindAllCurrentFav(userName).ToList();
        var genreListTemp = repository.FindAllGenres(userName).ToList();
        var userListTemp = repository.FindAllUsers().ToList();


        return View(new HomeViewModel() 
        { 
            topTenFavList = topTenList,
            currentFavList = currentList,
            genreList = genreListTemp,
            userList = userListTemp

        });
    }
2
  • 1
    What membership provider / method you use? If you are using the aspnet membershipProvider, you just need to [Authorize] your method, and to get username, simply use User.Identity.Name Commented Nov 18, 2011 at 1:17
  • yes im using the default asp.net membership but i need to pass the username as a parameter to the index action, so i can query the right model to pass it to my views Commented Nov 18, 2011 at 1:22

1 Answer 1

2

You actually don't need to pass it in if they're authenticated:

public ActionResult Index() 

{

string userName = User.Identity.Name;

var topTenList = repository.FindAllTopTen(userName).ToList();
var currentList = repository.FindAllCurrentFav(userName).ToList();
var genreListTemp = repository.FindAllGenres(userName).ToList();
var userListTemp = repository.FindAllUsers().ToList();


return View(new HomeViewModel() 
{ 
    topTenFavList = topTenList,
    currentFavList = currentList,
    genreList = genreListTemp,
    userList = userListTemp

});

}

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, intellisense didnt show me any hints that i could use such code in the controller, but this is what i needed

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.