views:

169

answers:

2

I am using the asp.net MVC Framework. IN my application a user has to log in. And when the combination of username and password is correct, the div (or panel?) with with the menu in it, must become visible. But how can I do this? When a name my panel pnlMenu, in my controller i cannot do something like:

pnlMenu.visible = true;

So, how do i have to do this?

A: 

Create a method or property on your View that enables you to hide or show the appropriate controls ?

Then, in your Controller you can access that property or method of your View, can't you ?

You do not want to reference specific 'controls' on your View in your controller, since, one of the ideas of MVC is that you can just replace the UI with another implementation (web / win / ...) and make use of the same controllers and application logic. Then, you just want to describe an operation that your View should support, so, in the interface that describes the 'contract' that your View must support, you should create a method which is called 'ChangeState( bool loggedIn )' for instance.

In the controller, you can call this method when the user has logged in.

Frederik Gheysels
How do I create a property on my View? The view is a aspx without a codebehind file right?
Martijn
"... so, in the interface that describes the 'contract' that your View must support, you should create a method which is called.." this part I don't understand. Where do I need to do this?
Martijn
You don't want to directly access methods or properties in your view from your controller.
James Avery
why not ? When you set 'ViewData', this is some common / shared data between view why this extra indirection ?http://www.mvcsharp.org/Getting_started_with_MVCSharp/Default.aspx
Frederik Gheysels
You don't want to call a method on the view because then you are tightly coupling your controller to the view it is showing. If you want to change the view later you would need to make sure that the view had this method or your code wouldn't even compile. With ViewData the view can simple ignore that data if it doesn't need it.
James Avery
+3  A: 

What you should do is in your controller check to see if a user is logged in and set a value in the ViewData like this:

ViewData["IsLoggedIn"] = true;

Then in your view you can set the visibility of the method based on this value. This way if you change the view later, or decide to have multiple views, they can each use this value and there isn't any coupling between your view and your controller.

James Avery