Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 为下拉列表阻止某些项目的步骤_Asp.net Mvc_Linq_Combobox - Fatal编程技术网

Asp.net mvc 为下拉列表阻止某些项目的步骤

Asp.net mvc 为下拉列表阻止某些项目的步骤,asp.net-mvc,linq,combobox,Asp.net Mvc,Linq,Combobox,我正在从事MVC项目,有一个下拉列表来填写国家名称。但是我想屏蔽一些国家的名字,这样 他们不会在下拉列表中反映出来。下面提到了我当前的代码。如果我不想在下拉列表中显示“利比里亚”,我应该在代码中添加什么 我正在发布在VisualStudio中运行函数调用的代码 private static void FillCombos(GuestInformationPresenter model) { FillCountryLists(model); } /// <summary> /

我正在从事MVC项目,有一个下拉列表来填写国家名称。但是我想屏蔽一些国家的名字,这样 他们不会在下拉列表中反映出来。下面提到了我当前的代码。如果我不想在下拉列表中显示“利比里亚”,我应该在代码中添加什么

我正在发布在VisualStudio中运行函数调用的代码

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

/// <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());
}

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

/// <summary>
/// Function to retrieve collection of Countries.
/// </summary>
/// <returns>collection of countries</returns>
public static KeyValuePair<string, string>[] 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);
}

// GuestInformationPresenter
public void FillCountryLists(Dictionary<string, string> countryList)
{
    this.CountryList = countryList;
}
私有静态空填充组合(GuestInformationPresenter模型)
{
填充列表(模型);
}
/// 
///函数在组合中填充国家/地区。
/// 
///GuestInformationPresenter对象的类型
私有静态void fillCountryList(GuestInformationPresenter模型)
{
//我想在这里添加一些Linq代码来阻止一些国家。
model.FillCountryList(ReservationService.RetrieveCountries());
}
公共静态KeyValuePair[]RetrieveCountries()
{
var countries=来自LookupManagerCache.Retrieve().CountryRows中的LookUpdatea.CountryRow CountryRow
orderby countryRow.Name
选择新的KeyValuePair(DataField.RetrieveValue(()=>countryRow.Code),DataField.RetrieveValue(()=>countryRow.Name));
返回国家。ToArray();
}
/// 
///用于检索国家/地区集合的函数。
/// 
///收集国家
公共静态KeyValuePair[]RetrieveCountries()
{
返回LookupManager.RetrieveCountries();
}
公共静态字典检索国家()
{
KeyValuePair[]国家=CruiseLookup.RetrieveCountries();
返回countries.ToDictionary(pair=>pair.Key,pair=>pair.Value);
}
//来宾信息演示者
公共无效国家列表(字典国家列表)
{
this.CountryList=CountryList;
}

据我所知,您的问题是关于LINQ的,您希望在选择时使用SQL not in子句。请参考类似问题

您也可以在选择器中添加一个
where
子句,将不相关的国家从列表中排除

大概是这样的:

        var countries = from LookupData.CountryRow countryRow in LookupManagerCache.Retrieve().CountryRows
                        where countryRow.Name != "Liberia"
                        orderby countryRow.Name
                        select new KeyValuePair<string, string>(DataField.RetrieveValue(() => countryRow.Code), DataField.RetrieveValue(() => countryRow.Name));
var countries=来自LookupManagerCache.Retrieve().CountryRows中的LookUpdatea.CountryRow CountryRow
countryRow.Name在哪里!=“利比里亚”
orderby countryRow.Name
选择新的KeyValuePair(DataField.RetrieveValue(()=>countryRow.Code),DataField.RetrieveValue(()=>countryRow.Name));

我已更新了相关代码。请您现在检查一下好吗?恐怕您的代码看起来有点混乱,有三种方法叫做
RetrieveCountries()
,这是怎么回事?另外:您是否尝试过我的建议,比如添加一个
where
子句来过滤掉您不想显示的国家/地区?返回countries.ToArray()的方法;检索国家/地区列表很常见,但当我希望此列表绑定GuestInformationPresenter/Controller时,我希望阻止这些国家/地区。所以你建议的解决方案会影响我的共同要求,我必须展示所有国家。好了,现在雾消散了。那么为什么不在相关的controlleraction中过滤
KeyValuePair
数组呢?它是一个简单的
数组
。因此,您只需确保使用System.Linq包含
在文件的开头,然后可以使用Linq对数组进行如下过滤:
countries=countries.Where(kv=>kv.Value!=“Liberia”).ToArray()(我还建议使用键而不是值,但这是您的决定)