在 MVC3 中使用多项选择向用户添加服务
我有一个问题,不知道从哪里开始,因为我是 MVC 新手。我有三个表:
- User(UserID、Username 等...)这定义了用户。
- Service(ServiceID、ServiceName 等...) .) 这定义了服务。
- 服务映射到用户。
许可证(ID、UserID、ServiceID 等...)将 最终,如果用户拥有许可证,则可以访问服务。理想情况下,我希望在我的 EditUser 视图中有一个服务列表,我可以在其中检查他们应该拥有哪些服务的许可证。
该列表需要预先填充当前许可证,如果未选中并保存该列表,则需要将其删除。
我拥有添加和删除许可证的所有方法,但我需要知道如何在我的控制器和视图中实现这一点。
提前致谢。
I have a problem and don't know where to start as I am new to MVC. I have three tables:
- User (UserID, Username, etc...) This defines users.
- Service (ServiceID, ServiceName, etc...) This defines services.
- Licenses (ID, UserID, ServiceID, etc...) Maps services to a user.
In the back-end the user can access a service if he has a license. Ideally I would like a list of services in my EditUser view where I could check which services they should have licenses to.
This list needs to pre-populate with current licenses and if one is unchecked and saved it needs to be deleted.
I have all the methods to add and remove licenses, but I need to know how to implement this in my controller and view.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,定义 ViewModel
视图模型只是一个辅助类,其中包含显示视图所需的所有内容。然后,在您的操作中:
然后,使用 EditUserViewModel 作为模型使您的视图成为类型化视图:
您可以将 EditUserViewModel 类重用于其他视图(例如 UserDetails)。在这种情况下,您可能需要重命名并删除“编辑”前缀。
更新以澄清评论中的问题:
经验法则:保持视图模型小、笨且简单。没有方法、功能或智能,只有几个在显示过程中为您提供帮助的属性。您只想在非常相似的视图上重用视图模型,例如
EditUser
和DisplayUser
视图的情况。您将为DisplayServices
视图等提供不同的视图模型。First of all, define a ViewModel
A View Model is simply a helper class that contains everything that you'll need to display a view. Then, in your action:
Then, make your view a typed view with EditUserViewModel for model:
You can reuse the EditUserViewModel class for other views, UserDetails, for instance. In that case you might want to rename and get rid of the "Edit" prefix.
UPDATE to clarify question in comments:
Rule of thumb: Keep your view models small, dumb and simple. No methods, functionality or intelligence, just a couple of properties that assist you in the display process. You'll only want to reuse View Models on views that are very similar, like would be the case with an
EditUser
andDisplayUser
view. You'd have a different view model for aDisplayServices
view, etc.