【发布时间】:2016-04-17 03:00:39
【问题描述】:
在我看来,这是我的剧本。
$(function () {
$('#buttonx').on("click", function (e) {
e.preventDefault();
$.ajax({
url: 'Ficha/VerificarPatrocinador',
contentType: 'application/json; charset=utf-8',
type: 'GET',
dataType: 'json',
data: {i: 100036},
success: function (data) {
$(data).each(function (index, item) {
//$('#NomePatr').append(item.Nome)
$("#NomePatr").val(item.Nome);
});
}
});
});
});
</script>
这是我对控制器的操作。
public JsonResult VerificarPatrocinador(int i)
{
var db = new FMDBEntities();
db.Configuration.ProxyCreationEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
var consulta = db.Tabela_Participante.Where(p => p.ID_Participante == i);
return Json(consulta.
Select(x => new
{
Nome = x.Nome
}).ToList(), JsonRequestBehavior.AllowGet);
}
我是 Ajax/Jquery 的新手,当我排除参数时没问题,但是,当我尝试将数据: {i: 100036} 放入我的脚本中并将参数放入我的操作中时。它不起作用。为什么会这样? 控制器运行良好。该参数甚至通过,但我无法在我的视图中返回此结果。 谢谢。
【问题讨论】:
-
删除
contentType: 'application/json; charset=utf-8',(您没有对数据进行字符串化)。您还应该使用url: '@Url.Action("/VerificarPatrocinador", "Ficha")',来确保正确生成您的网址
标签: javascript jquery json ajax asp.net-mvc