【问题标题】:Jquery Json The resource cannot be foundJquery Json 找不到资源
【发布时间】:2013-06-15 15:54:10
【问题描述】:

我正在尝试在 MVC 中使用 jquery 填充下拉列表。这是我下面的代码。

控制器:

[AcceptVerbs(HttpVerbs.Get)]
    public JsonResult GetTeams(StatisticModel model)
    {
        StatisticModel newModel = new StatisticModel(model.leagueId);
        var teams = newModel.getTeams;
        return Json(teams);
    }

查看:

<% using (Html.BeginForm("GetTeams", "Admin"))
    {%>
    <%: Html.ValidationSummary(true)%>
    <table class="addStatLeague">
    <tr><td>Choose League: </td><td><%: Html.DropDownListFor(model => model.leagueId, Model.getLeagues, new { @class = "dropdownlistLeagueStyle" })%></td><td><input id="leagueButton" class="loginButton" value="GetTeams" type="submit" /></td></tr>
</table>

<select id="LeagueTeams"></select>

查询:

$(function() {
$(".dropdownlistLeagueStyle").change(function () {
    $.getJSON("/Admin/GetTeams", { LeagueId: $(".dropdownlistLeagueStyle").val() },
  function (teams) {
      $("#LeagueTeams").empty();
      $.each(teams, function (i, team) {
         $("#LeagueTeams").append($("<option>" + team.Text + "</option>"));
     });
  });
  });
   })

由于某种原因,我得到了一个找不到资源的错误页面,如下所示。

找不到资源。

描述:HTTP 404。您正在寻找的资源(或其之一 依赖项)可能已被删除,名称已更改,或者是 暂时不可用。请查看以下 URL 并制作 确保拼写正确。

请求的 URL:/Admin/GetTeams

【问题讨论】:

  • 您在页面加载或在下拉菜单中选择项目时收到此错误?
  • 当我选择一个项目并按下 Get Team 按钮时
  • 好的,那么我的解决方案应该可以了。你能让它工作吗?
  • 好的,我应该使用 Html.Begin Form 吗?
  • $.getJSON 现在可以工作了吗?只点击按钮不起作用?

标签: jquery asp.net-mvc json select


【解决方案1】:

试试JsonRequestBehavior.AllowGet

    [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult GetTeams(StatisticModel model)
    {
        StatisticModel newModel = new StatisticModel(model.leagueId);
        var teams = newModel.getTeams;
        return Json(teams,  JsonRequestBehavior.AllowGet);
    }

要检查的另一件事是模型绑定:

$.getJSON("/Admin/GetTeams", { LeagueId: $(".dropdownlistLeagueStyle").val() }

应该是:

$.getJSON("/Admin/GetTeams", { model: {LeagueId: $(".dropdownlistLeagueStyle").val() }}

如果服务器端的StatisticModel.LeagueIdint,你必须在客户端使用parseInt

$.getJSON("/Admin/GetTeams", { model: {LeagueId: parseInt($(".dropdownlistLeagueStyle").val()) }}

更新:考虑使用更简单的解决方案:

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetTeams(int LeagueId)
{
    StatisticModel newModel = new StatisticModel(LeagueId);
    var teams = newModel.getTeams;
    return Json(teams,  JsonRequestBehavior.AllowGet);
}

客户端:

$.getJSON("/Admin/GetTeams", { LeagueId: parseInt($(".dropdownlistLeagueStyle").val()) }

【讨论】:

  • 我在联盟下拉列表中使用:
  • @BuyMy Bet:您必须确保控制器名称为“AdminController”,如果服务器端的 LeagueId 为 int,则客户端需要 parseInt
  • @BuyMy Bet:所以你需要 parseInt,检查我回答中的最后一个 $.getJSON
  • 似乎没有任何效果...可能是因为?
    选择联赛: td> model.leagueId, Model.getLeagues, new { @class= "dropdownlistLeagueStyle" })%>
    在帖子中查看我更新的视图
  • @BuyMy Bet:主要问题是路线不匹配,而不是视图。 jQuery 在将嵌套的 json 对象序列化为 url 编码的字符串时存在问题。在您的情况下,嵌套的 json 对象是 StatisticModel model,您可能使用了“POST”和 json 数据的技巧来摆脱它。有一个更简单的方法,考虑不使用StatisticModel model,直接使用int LeagueId。这样客户端的代码是$.getJSON("/Admin/GetTeams", { LeagueId: parseInt($(".dropdownlistLeagueStyle").val()) }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 2015-05-03
  • 2013-01-03
  • 2014-03-20
  • 2015-04-13
  • 2021-07-06
相关资源
最近更新 更多