MVC 中的 C# 处理 GUI
我正在使用 asp.net MVC 框架。 在我的应用程序中,用户必须登录。当用户名和密码的组合正确时,带有菜单的 div(或面板?)必须变得可见。 但我该怎么做呢? 当我的面板命名为 pnlMenu 时,在我的控制器中我无法执行以下操作:
pnlMenu.visible = true;
那么,我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该做的是在控制器中检查用户是否已登录,并在 ViewData 中设置一个值,如下所示:
然后在您的视图中,您可以根据该值设置方法的可见性。 这样,如果您稍后更改视图,或决定拥有多个视图,它们都可以使用该值,并且视图和控制器之间没有任何耦合。
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:
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.
在视图上创建一个方法或属性,使您能够隐藏或显示适当的控件?
然后,在您的控制器中,您可以访问视图的该属性或方法,不是吗?
您不想在控制器中引用视图上的特定“控件”,因为 MVC 的思想之一是您可以用另一种实现(web / win / ...)替换 UI 并利用相同的控制器和应用程序逻辑。
然后,您只想描述您的视图应该支持的操作,因此,在描述您的视图必须支持的“契约”的接口中,您应该创建一个名为“ChangeState( boolloggedIn)”的方法。
在控制器中,您可以在用户登录时调用此方法。
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.