【问题标题】:Populating checkbox using information passed from a model使用从模型传递的信息填充复选框
【发布时间】:2013-01-31 11:31:32
【问题描述】:

在我的项目中,我们允许用户注册不同的订阅费率,从而实现不同的功能。一些用户是免费订阅的,因此只有有限的功能,而付费用户可以使用他们的帐户执行额外的操作。此功能仍有待实现,但我正在创建关税详情视图。

我有一个“SubscriptionTariffs”表和一个“SubscriptionServices”表,后者为每个服务的前者的 ID 字段保存一个外键。在我看来,我想显示所有可能的服务,无论关税是否支持它们。如果它们可用于该关税,我希望选中一个复选框,反之亦然。

因此,对于免费关税,我希望显示如下内容,其中前七个选项显示为勾选,其他选项未标记但仍显示:

            <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @Language.ViewTransactions</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @Language.CreateTransactions</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @Language.Announcements</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @Language.Alerts</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @Language.Foldering</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @Language.TransactionDownload</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @Language.TransactionUpload</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @Language.EZApproval</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED />&nbsp; @Language.SupplyChainFinance</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED />&nbsp; @Language.HierarchicalOrgs</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED />&nbsp; @Language.WhiteLabeling</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED />&nbsp; @Language.MultiFormatDownload</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED />&nbsp; @Language.MultiFormatUpload</td></tr>
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED />&nbsp; @Language.VANInteroperability</td></tr>

我编写了以下内容以显示所有相关服务的关税:

        @{
            foreach (var SubscriptionService in this.Model.SubscriptionServices)
            {
                <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @(SubscriptionService.ServiceName != null ? SubscriptionService.ServiceName : "")</td></tr>
            }
        }

这很好用,但我需要显示所有服务并根据需要选中或取消选中它们。

如果有人能指出我正确的方向,我将不胜感激,我认为我应该能够编写 if-else 语句或进行检查,但我不完全确定。

【问题讨论】:

    标签: c# jquery asp.net asp.net-mvc-3


    【解决方案1】:

    大概在本例中this.Model.SubscriptionServices 是根据所选资费持有的服务,因此您只需添加未选中的复选框。

    最容易做到这一点的地方是在你的模型中,你应该为所有订阅服务添加另一个集合,然后在你的视图中你可以这样写:

    @{
        foreach (var SubscriptionService in this.Model.SubscriptionServices)
        {
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED checked />&nbsp; @(SubscriptionService.ServiceName != null ? SubscriptionService.ServiceName : "")</td></tr>
        }
        foreach (var SubscriptionService in this.Model.AllSubscriptionServices.Where( s => !this.Model.SubscriptionServices.Contains( s ) ) )
        {
            <tr><td style="padding:5px;"><input type="checkbox" DISABLED />&nbsp; @(SubscriptionService.ServiceName != null ? SubscriptionService.ServiceName : "")</td></tr>
        }
    }
    

    或者,如果您使用 LINQ to SQL 或其他一些 ORM 来访问您的数据模型(并传递这些模型直接添加到视图中)然后您可以向生成的部分类添加一个布尔值:public bool IsInSelectedTarriff { get; set; } 然后编写一个更复杂的查询来填充您的 Model.SubscriptionServices 集合:

    this.SubscriptionServices = (
        from sub in DataContext.SubscriptionServices
        select new SubscriptionServices()
        {
            ID = ID,
            ServiceName = ServiceName,
            // copy all data directly into it's field
            // ...
            IsInSelectedTarriff = sub.SubscriptionTarriffID == this.SubscriptionTarriff
        } );
    

    您应该注意,如果在另一个视图中使用此模型,最佳做法是为特定于该屏幕的 SubscriptionTarriff 创建一个新 ViewModel,它是原始属性的直接副本,并添加了 IsInSelectedTarriff选项。 Tuple 类也有助于将其他数据附加到集合中。

    【讨论】:

    • 这看起来很到位,我今天会看看如何实现它。非常感谢您的帮助,我会在整理后通知您。在此期间,我已经接受了您的回答。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多