使用Json.Encode从javascript调用webmethod参数

时间:2017-09-11 12:56:58

标签: c# asp.net-mvc

我有WebMethod

[WebMethod]
public static string[] getDropDown(int idSpacies)
{
    var raceList = from r in db1.breed where r.spaciesID == idSpacies select r;
    string[] temp = new string[raceList.Count()];
    int counter = 0;

    foreach (var item in raceList )
    {
        temp[counter] = item.name;
        counter++;
    }
    return temp;
}

和JavaScript功能:

function changeSpacies()
{
    var temp = document.getElementById("selectpicker").value;

    var availableTags = @Html.Raw(Json.Encode(AlewetWeb.Areas.ClinicService.Controllers.BreedsController.getDropDown(temp)));

      $("#tags").autocomplete({
        source: availableTags
      });
}

当我在temp WebMethod来电中使用getDropDown变量作为参数时,我收到错误

  

temp不存在于当前上下文中

如何将JavaScript变量传递给WebMethod

0 个答案:

没有答案