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);
}
これが私のWebフォーム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>
}