MVC3 AJAX级联DropDownLists

时间:2012-06-19 03:27:21

标签: asp.net-mvc-3 jquery

我很难弄清楚如何获得级联下拉列表以适用于我的asp.net mvc3应用程序。我有一个弹出框,我想显示2个下拉列表,第二个是根据第一个选择的内容填充。每次运行应用程序时,控制器方法都会返回正确的值列表,但是我没有点击ajax调用的成功部分,而是点击了错误部分。我已经做了很多研究,并按照我发现的几个例子,但有些事情仍然不太正确,我们将非常感谢任何帮助。

编辑: 使用firebug进一步检查显示错误500内部服务器错误,其中指出:异常详细信息:System.InvalidOperationException:序列化类型为'System.Data.Entity.DynamicProxies.GameEdition

的对象时检测到循环引用

我有以下jQuery / AJAX:

<script type="text/javascript">
$(function () {
    $("#PlatformDropDownList").change(function () {
        var gameId = '@Model.GameID';
        var platformId = $(this).val();
        // and send it as AJAX request to the newly created action 
        $.ajax({
            url: '@Url.Action("Editions")',
            type: 'GET',
            data: { gameId: gameId, platformId: platformId },
            cache: 'false',
            success: function (result) {
                $('#EditionDropDownList').empty();
                // when the AJAX succeeds refresh the ddl container with 
                // the partial HTML returned by the PopulatePurchaseGameLists controller action 
                $.each(result, function (result) {
                    $('#EditionDropDownList').append(
                        $('<option/>')
                            .attr('value', this.EditionID)
                            .text(this.EditionName)
                    );

                });
            },
            error: function (result) {
                alert('An Error has occurred');
            }
        });
    });
});

这是我的控制器方法:

  public JsonResult Editions(Guid platformId, Guid gameId)
  {
     //IEnumerable<GameEdition> editions = GameQuery.GetGameEditionsByGameAndGamePlatform(gameId, platformId);
     var editions = ugdb.Games.Find(gameId).GameEditions.Where(e => e.PlatformID == platformId).ToArray<GameEdition>();

     return Json(editions, JsonRequestBehavior.AllowGet);
  }

这是我的网页表格html:

<div id="PurchaseGame">
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true, "Please correct the errors and try again.")
    <div>
        <fieldset>
            <legend></legend>
            <p>Select the platform you would like to purchase the game for and the version of the game you would like to purchase.</p>

            <div class="editor-label">
                @Html.LabelFor(model => model.PlatformID, "Game Platform")
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.PlatformID, new SelectList(Model.Platforms, "GamePlatformID", "GamePlatformName"), new { id = "PlatformDropDownList", name="PlatformDropDownList" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.EditionID, "Game Edition")
            </div>
            <div id="EditionDropDownListContainer">
                @Html.DropDownListFor(model => model.EditionID, new SelectList(Model.Editions, "EditionID", "EditionName"), new { id = "EditionDropDownList", name = "EditionDropDownList" })
            </div>

            @Html.HiddenFor(model => model.GameID)
            @Html.HiddenFor(model => model.Platforms)

            <p>
                <input type="submit" name="submitButton" value="Purchase Game" />
                <input type="submit" name="submitButton" value="Cancel" />
            </p>

        </fieldset>
    </div>
}

1 个答案:

答案 0 :(得分:4)

您无法使用GET动词发送JSON编码请求。因此,将type: 'GET'替换为type: 'POST',它会起作用。此外,由于您已经指定了JSON请求,因此您必须发送使用JSON.stringify函数实现的JSON请求:data: JSON.stringify({ gameId: gameId, platformId: platformId }),。但由于你只有2个值,我认为使用GET会更容易。所以我的建议是删除contentType: 'application/json'参数并让你的AJAX请求如下所示:

$.ajax({
    url: '@Url.Action("Editions")',
    type: 'GET',
    data: { gameId: gameId, platformId: platformId },
    cache: 'false',
    success: function (result) {
        $('#EditionDropDownList').empty();
        // when the AJAX succeeds refresh the ddl container with 
        // the partial HTML returned by the PopulatePurchaseGameLists controller action 
        if(result.length > 0)
        {
            $.each(result, function (result) {
                $('#EditionDropDownList').append(
                    $('<option/>')
                         .attr('value', this.EditionID)
                         .text(this.EditionName)
                );
            });
        }
        else
        {
            $('#EditionDropDownList').append(
                $('<option/>')
                    .attr('value', "")
                    .text("No edition found for this game")
            );
        }

    },
    error: function () {
        alert('An Error has occured');
    }
});

同样在Razor标记的DropDownListFor助手中,我注意到以下内容:

onchange = "Model.PlatformID = this.value;"

我可以说的是,这并不是你想象的那样。


更新:

您似乎收到了循环对象引用错误,因为您正在将editions域模型传递给Json方法。循环引用对象层次结构不能是JSON序列化的。此外,您不需要通过将此版本中包含的所有垃圾发送给客户端来浪费带宽。您的所有客户需求都是ID和名称的集合。所以只需使用视图模型:

public ActionResult Editions(Guid platformId, Guid gameId)
{
    var editions = ugdb
        .Games
        .Find(gameId)
        .GameEditions
        .Where(e => e.PlatformID == platformId)
        .ToArray<GameEdition>()
        .Select(x => new 
        {
            EditionID = x.EditionID,
            EditionName = x.EditionName
        })
        .ToArray();

    return Json(editions, JsonRequestBehavior.AllowGet);
}