我在asp.net mvc3应用程序中很难想出如何使级联下拉列表起作用。我有一个弹出框,我想显示两个下拉列表,第二个下拉列表是基于第一个下拉列表所选内容填充的。每次运行应用程序时,控制器方法返回正确的值列表,但我没有命中ajax调用的成功部分,而是错误部分。我进行了大量研究并遵循了几个例子,但仍然存在问题,任何帮助将不胜感激。
编辑: 使用火狐浏览器检查后,显示内部服务器错误500 Exception Details: System.InvalidOperationException:在序列化类型为'System.Data.Entity.DynamicProxies.GameEdition'的对象时检测到循环引用
以下是我的jQuery / AJAX代码:
这是我的控制器方法:
这是我的网页表单HTML代码:
编辑: 使用火狐浏览器检查后,显示内部服务器错误500 Exception Details: 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>
}