小编最近做系统的时候,遇到了很多问题,虽然问题不难,但是多啊,虽然不难,但小编没有遇到过啊,于是,我开始了曲折的调试之旅。
问题之一:js有时无法接收controller传回的结果?这是为什么?
DAL
public List<ViewModel.addMcoinRecordViewModel> QueryLoadPersonScore(string date, string userName)
{
excellentmcoinEntities DBcontext = new excellentmcoinEntities();
List<ViewModel.addMcoinRecordViewModel> addmcoinList = new List<ViewModel.addMcoinRecordViewModel>();
addmcoinList = (from r in dbcontext.t_addmcoinrecord
join u in dbcontext.t_user on r.userID equals u.userID
where (r.date == date && u.userName == userName)
select new ViewModel.addMcoinRecordViewModel
{
addMcoin = r.addMcoin,
}
).ToList();
foreach (var item in addmcoinList)
{
item.AddMcoin = Convert.ToInt32(item.addMcoin);
}
int headmanbonus = 0;
int cancelbonus = 0;
if (addmcoinList.Count > 0 && addmcoinList!=null)
{
for (int i = 0; i < addmcoinList.Count; i++)
{
if (addmcoinList[i].AddMcoin >= 0)
{
headmanbonus += addmcoinList[i].AddMcoin;
}
else
{
cancelbonus += addmcoinList[i].AddMcoin;
}
}
addmcoinList[0].headmanbonus = headmanbonus;
addmcoinList[0].cancelbonus = cancelbonus;
addmcoinList[0].allbonus = headmanbonus+ cancelbonus;
}
else
{
return addmcoinList;
}
return addmcoinList;
}
#endregion
代码无非两种结果:
(1)从数据库查到数据,返回值为泛型
(2)从数据库查不到数据,返回值为null
当把结果通过BLL传回给controller时,也需要判断获得的结果,分别返回给JavaScript不同的结果。
controller
JavaScript
$(function () {
$.ajax({
//请求后台的URL
url: '/mobile_personScore/LoadPropertyBonusController',
// 数据发送方式
type: "POST",
// 接受数据格式
dataType: "text",
success: function (data) {
document.getElementById("propertybonus").value = data;
propertybonus = parseInt(data);
queryBonus();
}
})
})
function queryBonus() {
$.ajax({
//请求后台的URL
url: '/mobile_personScore/LoadBonusController',
// 数据发送方式
type: "POST",
// 接受数据格式
dataType: "json",
success: function (data) {
document.getElementById("headmanbonus").value = data[0].headmanbonus;
document.getElementById("cancelbonus").value = data[0].cancelbonus;
data[0].allbonus += propertybonus;
document.getElementById("allbonus").innerHTML = data[0].userName+"今日获得总分为:" + data[0].allbonus;
}
})
}
由于js“data”返回值总是显示“[object object]”,但无法获取确定的值,小编就认为问题出在js,于是一直在修改js获得结果的代码。后来经人指导,才知道问题出现在controller。当controller获得的值是null时,应该构造一个数组作为返回值(单步调试的结果其实已经告诉我了,只是我没看出来)。
所以,controller正确的代码是这样的:
public ActionResult BonusController(string date)
{
string userName = Session["userName"].ToString();
McoinBonusBLL mcoinbonusBLL = new McoinBonusBLL();
List<ViewModel.addMcoinRecordViewModel> addMcoinList = new List<ViewModel.addMcoinRecordViewModel>();
addMcoinList = mcoinbonusBLL.QueryLoadPersonScore(date,userName);
if (addMcoinList != null && addMcoinList.Count > 0)
{
//序列化返回值
var tempType = from c in addMcoinList
select new
{
headmanbonus = c.headmanbonus,
cancelbonus = c.cancelbonus,
allbonus = c.allbonus,
attrimcoin = c.Attrimcoin
};
return Json(tempType);//JsonResult
}
else
{
//序列化返回值
var tempType = new ViewModel.addMcoinRecordViewModel[]{
new ViewModel.addMcoinRecordViewModel()
{
headmanbonus = 0,
cancelbonus = 0,
}
};
return Json(tempType);//JsonResult
}
}
#endregion
小结
经过这一番调试,我觉得主要问题在于这一阶段的内容学的不扎实,对于一些基础知识没有一定的敏感程度,学习的内容没有形成知识网。接下来的安排就是把这些欠下的都补上来。