在 MVC3 中使用多项选择向用户添加服务

发布于 2024-10-21 08:09:14 字数 476 浏览 6 评论 0原文

我有一个问题,不知道从哪里开始,因为我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最好是你 2024-10-28 08:09:14

首先,定义 ViewModel

public class EditUserViewModel
{
    public User User { get; set; }
    public IList<License> Licenses { get; set; }
    public IList<Service> Services { get; set; }
}

视图模型只是一个辅助类,其中包含显示视图所需的所有内容。然后,在您的操作中:

public ActionResult EditUser(int id)
{
    var userViewModel = new EditUserViewModel
    {
        User = // Get user from db
        Licenses = // Get licenses for that user
        Services = // Getservies the user in entitled to   
    }

    return View(userViewModel); 
}

然后,使用 EditUserViewModel 作为模型使您的视图成为类型化视图:

@model EditUserViewModel

@* //Some html or whatever here *@

@* //Access your model properties as follows *@
@Model.User
@Model.Licenses
@Model.Service    

您可以将 EditUserViewModel 类重用于其他视图(例如 UserDetails)。在这种情况下,您可能需要重命名并删除“编辑”前缀。

更新以澄清评论中的问题:
经验法则:保持视图模型小、笨且简单。没有方法、功能或智能,只有几个在显示过程中为您提供帮助的属性。您只想在非常相似的视图上重用视图模型,例如 EditUserDisplayUser 视图的情况。您将为 DisplayServices 视图等提供不同的视图模型。

First of all, define a ViewModel

public class EditUserViewModel
{
    public User User { get; set; }
    public IList<License> Licenses { get; set; }
    public IList<Service> Services { get; set; }
}

A View Model is simply a helper class that contains everything that you'll need to display a view. Then, in your action:

public ActionResult EditUser(int id)
{
    var userViewModel = new EditUserViewModel
    {
        User = // Get user from db
        Licenses = // Get licenses for that user
        Services = // Getservies the user in entitled to   
    }

    return View(userViewModel); 
}

Then, make your view a typed view with EditUserViewModel for model:

@model EditUserViewModel

@* //Some html or whatever here *@

@* //Access your model properties as follows *@
@Model.User
@Model.Licenses
@Model.Service    

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 and DisplayUser view. You'd have a different view model for a DisplayServices view, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文