我有一个很难搞清楚如何让级联下拉列表,为我的asp.net MVC3应用工作。 我有一个弹出框,我想显示2个dropdownlists的基础上,什么是第一选择第二个被填充。 每次我运行应用程序的控制方法返回值的正确列表,但不是打Ajax调用的成功部分我打的错误部分。 我已经做了很多的研究和随后的几个例子,我发现,但事情仍然不完全正确,任何帮助将不胜感激。
编辑:使用萤火进一步检查示出一条错误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>
}