In my project we allow users to sign up to different subscription tariffs which allow for different functionality. Some users are on free subscriptions and as such will only have limited features, while paying users are able to perform extra actions with their account. This functionality is still to be implemented, but I am creating a view for details of the tariffs.
I have a “SubscriptionTariffs” table and a “SubscriptionServices” table, with the latter holding a foreign key to the ID field of the former for each service. In my view I want to display all possible services, regardless of whether the tariff supports them or not. If they are available on that tariff, I want a checkbox to be checked, and vice versa.
So for a free tariff, I would want something like the following to be displayed where the first seven options are displayed as ticked, and the others unmarked but still displayed:
<tr><td style="padding:5px;"><input type="checkbox" DISABLED checked /> @Language.ViewTransactions</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED checked /> @Language.CreateTransactions</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED checked /> @Language.Announcements</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED checked /> @Language.Alerts</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED checked /> @Language.Foldering</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED checked /> @Language.TransactionDownload</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED checked /> @Language.TransactionUpload</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED checked /> @Language.EZApproval</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED /> @Language.SupplyChainFinance</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED /> @Language.HierarchicalOrgs</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED /> @Language.WhiteLabeling</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED /> @Language.MultiFormatDownload</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED /> @Language.MultiFormatUpload</td></tr>
<tr><td style="padding:5px;"><input type="checkbox" DISABLED /> @Language.VANInteroperability</td></tr>
I have written the following to display all the associated services to a tariff:
@{
foreach (var SubscriptionService in this.Model.SubscriptionServices)
{
<tr><td style="padding:5px;"><input type="checkbox" DISABLED checked /> @(SubscriptionService.ServiceName != null ? SubscriptionService.ServiceName : "")</td></tr>
}
}
This works fine, but I need to display all services and check or uncheck them as necessary.
I would be extremely grateful if someone could point me in the right direction, I assume I should be able to write an if-else statement or something to check, but I’m not entirely sure.
presumably in this example
this.Model.SubscriptionServicesare the services which are held under the chosen tariff so you only need to add the unchecked checkboxes.The easiest place to do this would be in your model, you should add another collection for all subscription services, then in your view you could write:
alternatively, applying this where clause in your model would be more efficient as less data would be sent to the View, more efficient still, if you’re using LINQ to SQL or some other ORM to access your data model (and passing those models directly to the view) then you can add a boolean to the generated partial class:
public bool IsInSelectedTarriff { get; set; }then write a more complex query to fill your Model.SubscriptionServices collection:You should note though that if this model is used in another view, best practice would have you create a new ViewModel for the SubscriptionTarriff specific to that screen which is a direct copy of the properties in the original with the added
IsInSelectedTarriffoption. The Tuple class is also helpful for appending additional data to collections.