【发布时间】: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