concat在javascript中调用两个方法

时间:2016-11-10 17:03:11

标签: javascript java jquery ajax

我在我的所有代码中都使用了方法来填充下拉列表,如:

function FillSelectCatalog(method, controlName, valueName, textName, assignValue) {
    try {
        $(function () {
            $.ajax({
                cache: false,
                type: "post",
                dataType: "json",
                url: '../Catalogo/' + method,
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    FillSelect(controlName, response, valueName, textName, assignValue);
                },
                error: function (response) {
                    ErrorMessage("Error", GetTextError(response));
                }
            });
        });
    } catch (e) {
        ErrorMessage("Error", e.message);
    }
}

因此,使用jQuery,我将下拉列表称为:

FillSelectCatalog("GetActiveMeter", "meterEnergy", "Id", "Meter1", null);

在下拉列表中,我得到类似

的内容
meterEnegry1, 
meterEnergy2, 
etc

我希望与另一个FillSelectCatalog连接以获得类似"meterEnergy1 - Water"的内容,这可能吗?

我的另一个FillSelectCatalog与" Water"价值我想结束:

 FillSelectCatalog("GetMeterType", "meterType", "MeterTypeId", "Description", null);

控制器:

[Authorize]
        public string GetActiveMeter()
        {
            string toReturn = string.Empty;
            var userId = User.Identity.GetUserId();
            var currentUser = UserClass.GetUserBranchOfficeId(userId);
            try
            {
                IGenericRepository<Meter> entity = new GenericRepository<Meter>();
                IGenericRepository<MeterType> entity2 = new GenericRepository<MeterType>(); //there is Water value in column Description

                var result = entity.GetList(x => x.Status).Where(x => x.BranchOfficeId == currentUser);

                toReturn = new JavaScriptSerializer().Serialize(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return toReturn;
        }

"Water"值在名为MeterType的列中名为Description的另一个表中 此致

2 个答案:

答案 0 :(得分:0)

[Authorize]
public string GetActiveMeter()
{
    string toReturn = string.Empty;
    var userId = User.Identity.GetUserId();
    var currentUser = UserClass.GetUserBranchOfficeId(userId);
    try
    {
        IGenericRepository<Meter> entity = new GenericRepository<Meter>();
        var result = entity
          .GetList(x=>x.State, x => x.MeterType)
          .Where(x=>x.BranchOfficeId==currentUser)
          .Select(x=>new { Id = x.Id, Meter1 = x.Meter1 + ' - " + x.MeterType.Description});

        toReturn = new JavaScriptSerializer().Serialize(result);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return toReturn;
}

答案 1 :(得分:0)

您当然可以在某处保留一些mySelectValues[]并在值到来时插入值。对于Meter1,您可以预先添加,也可以为其他人添加。

var mySelectValues = [];
function FillSelectCatalog(method, controlName, valueName, textName, assignValue) {
    try {
        $(function () {
            $.ajax({
                cache: false,
                type: "post",
                dataType: "json",
                url: '../Catalogo/' + method,
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    if (testName == 'Meter1') { 
                        mySelectValues.unshift(response);
                    } else {
                         mySelectValues.push(response);
                    }
                    FillSelect(controlName, response, valueName, textName, assignValue);
                },
                error: function (response) {
                    ErrorMessage("Error", GetTextError(response));
                }
            });
        });
    } catch (e) {
        ErrorMessage("Error", e.message);
    }
}