C# 排序下拉列表
我有下拉列表,需要排序。我试过了,但我对此表示怀疑 我的代码看起来像C# 排序下拉列表,c#,asp.net,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc 4,我有下拉列表,需要排序。我试过了,但我对此表示怀疑 我的代码看起来像 public ActionResult GetAgencyState(string statecode) { _logger.Info("GetAgencyState: " + statecode); AuthUserProfile profile = _sessionHelper.Get<AuthUserProfile>(SessionConstant.LightA
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);
}
调用此方法的我的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.OrderBy(a => a.Value);
首先,不返回已排序的集合。您可以使用agenciesState.OrderByagenciesState=>agenciesState.Value;未将已排序集合分配给变量。这是必须的
var sorted = agenciesState.OrderBy(agenciesState => agenciesState.Value);
return Json(sorted, JsonRequestBehavior.AllowGet);
但是,代码还有其他问题,包括前一行
agenciesState = agenciesState.Count() == 0 ? null : agenciesState;
这意味着agenciesState可以为null,在这种情况下,下面的行将抛出异常
无论如何,你不应该还字典。控制器代码可以是
public ActionResult GetAgencyState(string statecode)
{
....
List<Light_AgenciesState> getAgenciesState = _mcpServiceHelper.GetAgenciesState(....);
var agenciesState = getAgenciesState.Select(a => new { Value = a.AgencyKey, Name = a.AgencyName });
}
然后在ajax函数中
var url = '@Url.Action("GetAgencyState", "Import")'; // dont hardcode!
var ownerAgency = $("#OwnerAgency"); // cache it
$("#Ownerstate").change(function() {
var statecode = $(this).val();
if (!statecode) {
return;
}
$.get(url, { statecode: statecode }, function (data) {
ownerAgency.empty(); // remove existing options
$.each(data, function(index, item) {
ownerAgency.append($('</option>').val(item.Value).text(item.Name));
});
});
});
并从html标记中删除onchange=ownerstate函数。错误到底是什么?我猜您指的是agenciesState=agenciesState.OrderBy。。?但是如果这是为了填充下拉列表,为什么要返回字典?你是说值是选项显示文本,键是选项值吗?在前一行中,您可以潜在地将agenciesState设置为null-如何在null上调用OrderBy-它只会抛出一个异常。它不会在控制器代码中排序!并显示ajax调用您不应该返回字典-只需使用var agenciesState=getAgenciesState.Selecta=>new{value=a.AgencyKey,name=a.AgencyName};返回JsonagenciesState。。。;在ajax调用中-$.eachdata,functionindex,item{$OwnerAgency.append$.valitem.Value.textitem.Name;向我展示您为DropDown编写的razor代码为什么要使用javascript?@Html.DropDownListddlName,new SelectListYourList,DataValue,DataKey;语法足以满足您的需求。
var url = '@Url.Action("GetAgencyState", "Import")'; // dont hardcode!
var ownerAgency = $("#OwnerAgency"); // cache it
$("#Ownerstate").change(function() {
var statecode = $(this).val();
if (!statecode) {
return;
}
$.get(url, { statecode: statecode }, function (data) {
ownerAgency.empty(); // remove existing options
$.each(data, function(index, item) {
ownerAgency.append($('</option>').val(item.Value).text(item.Name));
});
});
});