我想按名称订购我的货币代码(名称),而不是ID。
我在每个货币的数据库ID和代码中都有。
在asp.net中我有这段代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IGP.Presentation.BOPortalFacade;
using Portal.Framework.Mvc.Extensions;
using IGP.Common.Windsor;
using IGP.Presentation.BOPortalFacade.DTOs.GlobalizationService;
using System.Web.Mvc;
using Portal.Framework.Services.Resources;
namespace Portal.Framework.Mvc.ViewDataPreparers
{
public class CurrenciesCodesPreparer : ActionFilterAttribute, IDataPreparer<Currency>
{
public IList<Currency> GetDataItems()
{
return DefaultContainerReference.Resolve<IGlobalizationService>().GetAllCurrencies().CurrencyList;
}
public static string ViewDataKey { get { return "CurrenciesCodes"; } }
public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
filterContext.Controller.ViewData[ViewDataKey] = GetDataItems().ToSelectableList(true, Resources.Default_All, null, null, "Id", "Code");
}
}
}
我尝试添加以下代码
filterContext.Controller.ViewData[ViewDataKey] = GetDataItems().OrderBy(x => x.Code).ToSelectableList(true, Resources.Default_All, null, null, "Id", "Code");
ToSelectableList有以下代码:
public static IList<SelectListItem> ToSelectableList<T>(
this IList<T> items,
bool addDefaultItem = true,
string defaultItemName = DefaultItemName,
int? defaultItemValue = DefaultItemValue,
object selectedItemValue = null,
string defaultDataValueField = DefaultDataValueField,
string defaultDataTextField = DefaultDataTextField,
bool selectAllItems = false)
{
// resolve from resources
if (defaultItemValue.Equals(DefaultItemName))
defaultItemName = Resources.GetResourceValue(AllResourceKey);
var resultList = new List<SelectListItem>();
if (items != null && items.Count > 0)
{
var itemSelectList = new SelectList(items, defaultDataValueField, defaultDataTextField, selectedItemValue);
resultList =
itemSelectList.Select(
x =>
new SelectListItem
{
Text = x.Text.ToString(CultureInfo.InvariantCulture),
Value = x.Value.ToString(CultureInfo.InvariantCulture)
}).ToList();
}
if ((selectAllItems) && (!addDefaultItem) && (selectedItemValue == null)) //in case you have multi select
{
foreach (var selectListItem in resultList)
{
selectListItem.Selected = true;
}
}
if (addDefaultItem)
{
resultList.Insert(
0,
new SelectListItem { Text = defaultItemName, Value = defaultItemValue == null ? "" : defaultItemValue.ToString() });
}
return resultList;
}
答案 0 :(得分:1)
您的扩展程序ToSelectableList()
接受IList<>
类型的第一个参数而不是IOrderedEnumerable<>
。您应该将扩展名定义重写为
public static IList<SelectListItem> ToSelectableList<T>(
this IEnumerable<T> items,
bool addDefaultItem = true,
string defaultItemName = DefaultItemName,
int? defaultItemValue = DefaultItemValue,
object selectedItemValue = null,
string defaultDataValueField = DefaultDataValueField,
string defaultDataTextField = DefaultDataTextField,
bool selectAllItems = false)
因此,它不会在使用IOrderedEnumerable<>