我试图通过使用jquery来监听更改来更新复选框列表。出于某种原因,控制器方法似乎没有返回JSON。我将此代码基于
this example
。
在该示例中,控制器调用
return Json(data, JsonRequestBehavior.AllowGet);
,但我只能
return Json(data)
工作我的理论是,返回类型必须特别允许GET,但我不知道如何使其工作。
我的代码工作正常,没有错误,所有3个控制台语句都显示在web浏览器中。
我的jquery代码(在我看来):
$('#lgcheese').change(function() {
ddToPopulate.empty();
console.log('change detected');
$.getJSON(CoIdUrl, { CoName: $(this).val() }, function (data) {
console.log('in shit');
if (!data) { console.log('no json returned');return;}
ddToPopulate.append($('<ListItem></ListItem>').val('').text('Please select'));
$.each(data, function(index, item) {
ddToPopulate.append($('<ListItem></ListItem>').val(item.Value).text(item.Text));
});
});
});
在我看来,后来我呼吁:
<CheckBoxList ID="cheesebox"></CheckBoxList>
要创建复选框列表,jquery应该填充。
我的控制器具有以下方法:
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
[HttpGet]
public JsonResult FetchCoIds(string CoName)
{
var data = new { Value = "val", Text = CoName+"_val1" };
return Json(data);
}