0

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?

0

2 Answers 2

3

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.

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

Comments

0

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.

5 Comments

How do I create a property on my View? The view is a aspx without a codebehind file right?
"... 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?
You don't want to directly access methods or properties in your view from your controller.
why not ? When you set 'ViewData', this is some common / shared data between view & controller; why this extra indirection ? mvcsharp.org/Getting_started_with_MVCSharp/Default.aspx
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.

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.