简体   繁体   中英

To block some items in dropdown List

I am working on MVC project and have a dropdown to fill with Country Name. But I want to block some country names so that they will not reflect in that dropdown. For blocked countries value of key should be listed in 'web.config' file. I do not want hard code. My current code is mentioned below. It will looks a little mixed up.

Step 1

private static void FillCombos(GuestInformationPresenter model)
{
    FillCountryLists(model);        
}

Step 2 // This code is written in GuestInformation Controller

/// <summary>
/// Function to fill countries in combos.
/// </summary>
/// <param name="model">GuestInformationPresenter type of object</param>
private static void FillCountryLists(GuestInformationPresenter model)
{
    //I want to add some Linq code here to Block some countries. 
    model.FillCountryLists(ReservationService.RetrieveCountries());
}

Step 3 // This code is written in GuestInformation Presenter

public void FillCountryLists(Dictionary<string, string> countryList)
{
    this.CountryList = countryList;
}    

Step 4 // Function to retrieve collection of Countries. /// /// collection of countries public static KeyValuePair[] RetrieveCountries() { return LookupManager.RetrieveCountries(); }

public static Dictionary<string, string> RetrieveCountries()
{
    KeyValuePair<string, string>[] countries = 
        CruiseLookup.RetrieveCountries();
    return countries.ToDictionary<KeyValuePair<string, string>, string, string>(pair => pair.Key, pair => pair.Value);
}

Step 5 // This code is written in LookManager.cs

public static KeyValuePair<string, string>[] RetrieveCountries()
{
    var countries = from LookupData.CountryRow countryRow in LookupManagerCache.Retrieve().CountryRows
    orderby countryRow.Name
    select new KeyValuePair<string, string>(
        DataField.RetrieveValue(() => countryRow.Code),
        DataField.RetrieveValue(() => countryRow.Name));

    return countries.ToArray();
}

The method that does return countries.ToArray(); (Step 5) is common to retrieve country list but when I want this list to bind GuestInformation Controller then I want to block those countries.

Currently from step 4 I am getting key-value pair of different countries. For example if country is Guiena then Key-value pair from step 4 will be key='GU' value='GUIENA'

After that in step 3 I am using this code to remove 'Guiena' country

public void FillCountryLists(Dictionary<string, string> countryList)
{   
    countryList.remove(key="GU");
    countryList.remove(key="XYZ");  // Here XYZ is key of any other country
    this.CountryList = countryList;
}

But I want to add some LINQ or other code in step 2 (at controller level) to remove countries.

The main thing that I want is that for blocked countries value of key should be listed in 'web.config' file . Can you please assist me to fix it?

You can use Contains like this:

var excludeKeys = new string[] { "GU", "XYZ" };
var countries = from ....
                where !excludeKeys.Contains(your key field)
                orderby ....
                select ....;

So in order your codes, you need to pass excludeKeys to your RetrieveCounties method and use them in query.

Step 2

private static void FillCountryLists(GuestInformationPresenter model)
{
    var excludeKeys = new string[] { "GU", "XYZ" }; 
    model.FillCountryLists(ReservationService.RetrieveCountries(excludeKeys));
}

Step 5

public static KeyValuePair<string, string>[] RetrieveCountries(List<string> excludeKeys)
{
    var countries = from ....
                    where !excludeKeys.Contains(your key field)
                    orderby ....
                    select ....;
    return ....
}

It's enough to add an empty list to RetrieveCountries method to don't filter countries.

As mentioned in comments and in Ehsan Sajjad's answer, you can store these values as a comma separated string in web.config or a setting.setting file then retrieve it, split it, put in list and then pass to your method.

In your web.config add the list of blocked countries, or if you don't have that section add it under configuration section:

<configuration>
  <appSettings>
    <add key="BlockedCountries" value="GU,XYZ"/>
  </appSettings>
</configuration>

and then in your controller:

 using System.Web.Configuration;
 ..............
 ..............




var blockedCountries =  WebConfigurationManager.AppSettings["BlockedCountries"].Split(',',StringSplitOptions.RemoveEmptyEntries);

var result =  model.countries.Where(x=> !blockedCountries.Contains(x.Key)).ToArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM