$ .evalJSON不是一个函数

时间:2011-03-21 20:55:27

标签: c# jquery asp.net ajax asp.net-mvc-3

这是我的jquery:

$(document).ready(function () {

    $("#GameId").change(function () {

        $.get('/MatchManager/GetMatchType/' + $(this).val(), function (response) {

            var Games = $.evalJSON(response);

            var ddlSelectedProduct = $("#MatchTypeId");

            $("#MatchTypeId > option").remove();

            for (i = 0; i < Games.length; i++) {

                ddlSelectedProduct.append($("<option />").val(Games[i].Value).text(Games[i].Text));

            }
        });

    });

});

我打印出响应及其正确,但由于某种原因,我的程序停在$.evalJson并说$.evalJSON is not a function这是我的GetMatchType控制器,以防万一:

public string GetMatchType(int id)
    {
        var ListMatchTypes = new List<SelectListItem>();
        using (var db = new MatchGamingEntities())
        {

            var MyMatchTypes = from m in db.MatchTypes
                               where m.GameId == id
                               select m;
            foreach (var item in MyMatchTypes.ToList())
            {
                ListMatchTypes.Add(new SelectListItem() { Text = item.MatchTypeName, Value = item.MatchTypeId.ToString() });
            }
        }
        return new JavaScriptSerializer().Serialize(ListMatchTypes);

    }

这是我的观点:

@using(Html.BeginForm()){     @ Html.ValidationSummary(真)              MatchModel                      @ Html.LabelFor(model =&gt; model.GameId)                               @ Html.DropDownList(“GameId”,新的SelectList(ViewBag.MyGames为System.Collections.IEnumerable,“GameId”,“GameName”),“请选择一个”)

    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.MatchTypeId)
    </div>
    <div class="editor-field">
        @Html.DropDownList("MatchTypeId", new SelectList(ViewBag.MatchTypes as System.Collections.IEnumerable, "Value", "Text"))

    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.MatchName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.MatchName)
        @Html.ValidationMessageFor(model => model.MatchName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.MatchDescription)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.MatchDescription)
        @Html.ValidationMessageFor(model => model.MatchDescription)
    </div>



    <div class="editor-label">
        @Html.LabelFor(model => model.Wager)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Wager)
        @Html.ValidationMessageFor(model => model.Wager) <br />
        <span>Your Current Account Balance: @ViewBag.Balance</span>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

}

    @ Html.ActionLink(“返回列表”,“索引”)

4 个答案:

答案 0 :(得分:3)

说明:

  • 控制器操作应始终返回ActionResults
  • 使用JsonResult而不是使用JavaScriptSerializer手动序列化
  • 您无需手动解析客户端上的任何$.evalJSON =&gt;如果你按照我之前的评论,将发送正确的内容类型application/json,jQuery将自动解析传递给成功回调的对象。
  • 永远不要在javascript =&gt;中对网址进行硬编码在处理url时总是使用url helper,或者由于添加了虚拟目录而导致部署时代码可能会中断。

这就是说让我们尝试从控制器操作开始改进代码:

public ActionResult GetMatchType(int id)
{
    using (var db = new MatchGamingEntities())
    {
        return Json(
            from m in db.MatchTypes
            where m.GameId == id
            select new {
                Text = m.MatchTypeName,
                Value = m.MatchTypeId
            },
            JsonRequestBehavior.AllowGet
        );
    }
}

然后是javascript:

$(function () {
    $('#GameId').change(function () {
        var url = '@Url.Action("GetMatchType", "MatchManager")';
        var data = { id: $(this).val() };
        $.get(url, data, function (games) {
            var ddlSelectedProduct = $('#MatchTypeId');
            ddlSelectedProduct.empty();
            $.each(games, function(index, game) {
                ddlSelectedProduct.append(
                    $('<option/>', {
                        value: game.Value,
                        text: game.Text
                    })
                );
            });
        });
    });
});

答案 1 :(得分:1)

不要试图自己解析JSON,而是让jQuery处理它。

只需将您的$.get替换为$.getJSON

即可 然后,jQuery将自动尝试将响应解码为JSON,您可以像普通对象一样自动访问它。

文档上的

jQuery.getJSON

答案 2 :(得分:0)

答案 3 :(得分:0)

$(document).ready(function () {
    $("#GameId").change(function () {
        $.getJSON('/MatchManager/GetMatchType/' + $(this).val(), function (Games) {
            var ddlSelectedProduct = $("#MatchTypeId");
            $("#MatchTypeId > option").remove();
            for (i = 0; i < Games.length; i++) {
                ddlSelectedProduct.append($("<option />").val(Games[i].Value).text(Games[i].Text));
            }
        });
    });
});