我第一次尝试 Json,我不确定我在这里做错了什么,但我的成功回调永远被调用。我有一个名为 RIPreset 的下拉菜单,它在更改时调用以下代码。我知道 on change 部分工作正常。在我的 alert("popup1") 代码运行下面的代码中,我的代码后面也有一个断点,可以看到 getPreset 方法被调用并将字符串传递给结果,但我的 . getJSON 调用。即,alert("popup2") 永远不会被调用。我认为这意味着我没有从我的 JsonResult 传递有效数据,但我不确定我做错了什么。任何帮助,将不胜感激。
代码背后
public JsonResult getPreset(int id)
{
RIPreset ripreset = db.RIPresets.Find(id);
return Json(new { Description = ripreset.Description, LaborHours = ripreset.LaborHours, HourlyRate = ripreset.HourlyRate, Amount = ripreset.Amount });
}
jQuery/Json
<script type="text/javascript">
$(document).ready(function () {
$("#RIPreset").change(function () {
var selection = $("#RIPreset").val();
alert("popup1");
$.getJSON('@Url.Action("getPreset")', { id: selection }, function (ripreset) {
alert("popup2");
$("#txtDescription").val(ripreset.Description);
$("#txtHourlyRate").val(ripreset.HourlyRate);
$("#txtLaborHours").val(ripreset.LaborHours);
$("#txtAmount").val(ripreset.Amount);
});
});
});
</script>