我有下拉列表,我需要排序。我嘗試過,但我對此抱有懷疑。排序下拉列表
我的代碼看起來像,
public ActionResult GetAgencyState(string statecode)
{
_logger.Info("GetAgencyState: " + statecode);
AuthUserProfile profile = _sessionHelper.Get<AuthUserProfile>(SessionConstant.LightAUP);
List<Light_AgenciesState> getAgenciesState = _mcpServiceHelper.GetAgenciesState(
profile.au_key.ToString(), profile.officernode.ToString(), profile.role, statecode);
Dictionary<string, string> agenciesState = new Dictionary<string, string>();
agenciesState = getAgenciesState.ToDictionary(x => x.AgencyKey.ToString(), x => x.AgencyName);
agenciesState = agenciesState.Count() == 0 ? null : agenciesState;
agenciesState.OrderBy(agenciesState => agenciesState.Value);
return Json(agenciesState, JsonRequestBehavior.AllowGet);
}
我得到一個錯誤,在這條線:
agenciesState.OrderBy(agenciesState => agenciesState.Value);
我的JavaScript代碼調用該方法:
var GetAgencyStateUrl = "/Import/GetAgencyState";
function OwnerStateFunction() {
var selectedVal = $("option:selected", $("#Ownerstate")).attr("Value");
if (selectedVal != "- select -" && selectedVal != "") {
$.get(GetAgencyStateUrl, { 'statecode': selectedVal }, function (data) {
$('#AgentPhone').val("");
$.map(data, function (value, key) {
var opt = "<option value=\"" + key + "\">" + value + "</option>";
$("#OwnerAgency").append(opt);
});
}
如何實現分類?
究竟是什麼錯誤? –
我假設你的意思是'agenciesState = agenciesState.OrderBy(..)'?但如果這是爲了填充下拉菜單,爲什麼你要返回一本字典? –
你的意思是值是選項顯示文本,關鍵是選項值?在前面的行中,您可能會將'agenciesState'設置爲null - 您可以在'null'上調用'OrderBy()'的方式 - 它只會引發異常。 –