這裏是我的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(TRU E) MatchModel @ Html.LabelFor(型號=> model.GameId) @ Html.DropDownList( 「遊戲ID」,新的SelectList(ViewBag.MyGames如System.Collections.IEnumerable, 「遊戲ID」,「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(」 返回目錄」, 「索引」)
有什麼不對$ .parseJSON? – 2011-03-21 21:16:32