我有一个HttpPost
请求发送回一个对象Value
。
我想让 ComputerLocation
div 在对象 Value
为 true(s.IsComputer
为 bool 值)时出现。
目前没有任何反应。
我尝试使用 Firebug 对其进行调试,并验证该请求实际上发回了对象 Value:true
,但是当我检查我的 result.Value
时,值
显示为未定义。
请检查我做错了什么?
脚本:
<script type='text/javascript'>
$(document).ready(function () {
$('#typeddl').on('change', function () {
$.ajax({
type: 'POST',
url: '@Url.Action("GetItemTypeForm")',
data: { itemTypeId: $('#typeddl').val() },
success: function (result) {
$('#ComputerLocation').toggle(result.Value === true);
}
});
});
$('#typeddl').trigger('change');
});
</script>
Json:
[HttpPost]
public JsonResult GetItemTypeForm(int itemTypeId)
{
//pseudo code
var data = from s in db.ItemTypes.ToList()
where s.ItemTypeId == itemTypeId
select new { Value = s.IsComputer };
return Json(data);
}
最佳答案
使用First
方法来获取单个结果,因为您的查询返回 IQueryable<T>
var data = (from s in db.ItemTypes.ToList()
where s.ItemTypeId == itemTypeId
select new { Value = s.IsComputer }).First();
然后像这样返回结果:
return Json( new { Value = data.Value });
关于c# - 如何正确获取Json值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21857033/