要阻止下拉列表

时间:2015-09-12 18:48:45

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

我正在开发MVC项目,并有一个下拉列表来填写国家/地区名称。但我想阻止一些国家名称,以便他们不会反映在该下拉列表中。对于阻止的国家/地区,键值应列在“web.config”文件中。我不想要硬编码。我目前的代码如下所述。它看起来有点混乱。

第1步

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

步骤2 //此代码编写在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());
}

步骤3 //此代码编写在GuestInformation Presenter

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

步骤4 //检索国家集合的功能。     ///     ///国家集合     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);
}

步骤5 //此代码以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();
}

执行return countries.ToArray();(步骤5)的方法通常用于检索国家/地区列表,但是当我希望此列表绑定GuestInformation Controller时,我想阻止这些国家/地区。

目前,从第4步开始,我获得了不同国家/地区的键值对。例如,如果country是Guiena,那么第4步中的Key-value对将是 key ='GU'value ='GUIENA'

在第3步之后,我使用此代码删除'Guiena'国家/地区

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;
}

但我想在步骤2中添加一些LINQ或其他代码(在控制器级别)以删除国家/地区。

我想要的主要是对于被阻止的国家/地区,键值应列在“web.config”文件中。你能帮我解决一下吗?

2 个答案:

答案 0 :(得分:1)

你可以使用像这样的包含:

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

因此,为了您的代码,您需要将excludeKeys传递给RetrieveCounties方法并在查询中使用它们。

第2步

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

第5步

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

RetrieveCountries方法添加空列表就足以过滤国家/地区了。

正如评论和Ehsan Sajjad的回答中所提到的,您可以将这些值存储为web.config中的逗号分隔字符串或setting.setting文件,然后将其检索,拆分,放入列表然后传递给您的方法。

答案 1 :(得分:0)

在您的web.config中添加被阻止的国家/地区列表,或者如果您没有将该部分添加到配置部分下:

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

然后在你的控制器中:

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




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

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