【问题标题】:To block some items in dropdown List阻止下拉列表中的某些项目
【发布时间】:2015-12-09 03:17:15
【问题描述】:

我正在开发 MVC 项目,并且有一个下拉菜单可以填写国家/地区名称。但我想屏蔽一些国家名称,这样它们就不会反映在该下拉列表中。对于被阻止的国家/地区,键的值应列在“web.config”文件中。我不想要硬代码。下面提到了我当前的代码。看起来会有点混乱。

第一步

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 步 // 检索国家/地区集合的函数。 /// /// 国家集合 公共静态 KeyValuePair[] RetrieveCountries() { 返回 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 // 这段代码写在 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 步开始,我正在获取不同国家/地区的键值对。例如,如果国家是 Guiena,那么第 4 步中的键值对将是 键='GU' 值='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 步中删除它们?
  • 你可以将它们添加到 web.config 并在控制器中读取
  • @EhsanSajjad 使用一些 Linq 是不可能的。我想要的主要事情是,对于被阻止的国家/地区,密钥的值应该列在“web.config”文件中。我是 MVC 新手,能否请您添加一些代码帮助?提前致谢。
  • 是的,有可能,您需要搜索如何在web.config中使用自定义键以及如何阅读它们
  • 对于这样一个简单的问题,您给我们的信息太多了。您只需要询问如何使用另一个集合过滤掉一个集合中的数据 - 您可以搜索并快速找到答案。

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


【解决方案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 方法中添加一个空列表就足够了,不过滤国家/地区。

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

【讨论】:

  • 据我了解,您要求在第 5 步添加代码。但我不想在此级别排除国家/地区。我想在控制器级别排除国家/地区 我们必须在“web.config”文件中添加什么代码?
  • 亲爱的,我可以知道您所说的“您的关键字段”是什么意思吗?我无法得到它。
  • 感谢您的帮助。按照 Ehsan Sajjad 的建议,我将在 web.config 中将键值添加为逗号分隔的字符串。但是我如何为您的代码检索它们。您能否为我逐步添加代码,因为我对 MVC 世界比较陌生,所以希望得到详细的帮助。
  • @Web34 该帖子似乎回答了您的问题,如果您接受/投票答案,那就太好了。这根本不是强制性的,但它使答案对未来的读者更有用,这是你的好意:)
【解决方案2】:

在您的 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();

【讨论】:

  • @Ehsan。如果我问的是愚蠢的事情,我深表歉意。有一个疑问,如果我使用您的解决方案,那么是否需要添加 Reza Aghaei 建议的代码以及“var 结果”的用途在控制器中?
  • 您也可以使用该代码,我只是向您展示了如何根据来自 web.config @web34 的值过滤记录
  • 但是没有办法添加 var result = model.countries.Where(x=> !blockedCountries.Contains(x.Key)).ToArray();在控制器中。不工作,请您重新检查一下。
  • 有没有办法?你的意思是什么?
  • 国家选项没有反映在那里。如何将“blockedCounties”值传递给演示者(在第 3 步),以便我可以通过添加代码 countryList.Remove(key:blockedcountriesKey); 在演示者级别排除值
猜你喜欢
  • 2014-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多