MVC字符串转json,ajax接受json返回值

时间:2022-02-04 23:42:50
#region 功能
/// <summary>
/// 查询 微信用户一定年月的账单
/// </summary>
/// <param name="year">年,最多4个字符</param>
/// <param name="month">月,最多2个字符</param>
/// <returns></returns>
public JsonResult YearDay(string year, string month)
{
if (UserStatus() == true)
{
#region 查询账单
string sql = "SELECT Q054,Q055,Q056,Q057,Q058,Q062,Q063,Q064,Q065,Q066,Q067,Q068,Q069,Q070,Q071,Q072,Q073,Q074,Q075 FROM skt75 where Q057 = '" + userid().Community2 + "' and Q058 = '" + userid().Community + "' and Q056= '" + year + "-" + month + "' ";
MySqlConnection con = new MySqlConnection("server=012.345.678.901;port=0123;User Id=UId;password=Pwd;database=DB");
MySqlDataAdapter da = new MySqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dts = ds.Tables[];
if (dts.Columns.Count > )
{
try
{
string Q063 = (dts.Rows[]["Q063"].ToString() == "False" ? "否" : "是");
string str = "{ 'Q054': '" + dts.Rows[]["Q054"].ToString() + "'," +
"'Q055':'" + dts.Rows[]["Q055"].ToString() + "'," +
"'Q062':'" + dts.Rows[]["Q062"].ToString() + "'," +
"'Q063':'" + Q063 + "'," +
"'Q064':'" + dts.Rows[]["Q064"].ToString() + "'," +
"'Q065':'" + dts.Rows[]["Q065"].ToString() + "'," +
"'Q066':'" + dts.Rows[]["Q066"].ToString() + "'," +
"'Q067':'" + dts.Rows[]["Q067"].ToString() + "'," +
"'Q068':'" + dts.Rows[]["Q068"].ToString() + "'," +
"'Q069':'" + dts.Rows[]["Q069"].ToString() + "'," +
"'Q070':'" + dts.Rows[]["Q070"].ToString() + "'," +
"'Q071':'" + dts.Rows[]["Q071"].ToString() + "'," +
"'Q072':'" + dts.Rows[]["Q072"].ToString() + "'," +
"'Q073':'" + dts.Rows[]["Q073"].ToString() + "'," +
"'Q074':'" + dts.Rows[]["Q074"].ToString() + "'," +
"'Q075':'" + dts.Rows[]["Q075"].ToString() + "'," +
"'Q056':'" + dts.Rows[]["Q056"].ToString() + "'" +
"}";
ViewBag.fang = userid().Community;
return Json(str.Replace('\'', '"'));
}
catch
{
string str = "{ " +
"'Q054':'暂无信息'," +
"'Q055':'暂无信息'," +
"'Q062':'暂无信息'," +
"'Q063':'暂无信息'," +
"'Q064':'暂无信息'," +
"'Q065':'暂无信息'," +
"'Q066':'暂无信息'," +
"'Q067':'暂无信息'," +
"'Q068':'暂无信息'," +
"'Q069':'暂无信息'," +
"'Q070':'暂无信息'," +
"'Q071':'暂无信息'," +
"'Q072':'暂无信息'," +
"'Q073':'暂无信息'," +
"'Q074':'暂无信息'," +
"'Q075':'暂无信息'," +
"'Q056':'暂无信息'" +
"}";
ViewBag.fang = userid().Community;
return Json(str.Replace('\'', '"'));
}
}
else
{
string str = "{ " +
"'Q054':'暂无信息'," +
"'Q055':'暂无信息'," +
"'Q062':'暂无信息'," +
"'Q063':'暂无信息'," +
"'Q064':'暂无信息'," +
"'Q065':'暂无信息'," +
"'Q066':'暂无信息'," +
"'Q067':'暂无信息'," +
"'Q068':'暂无信息'," +
"'Q069':'暂无信息'," +
"'Q070':'暂无信息'," +
"'Q071':'暂无信息'," +
"'Q072':'暂无信息'," +
"'Q073':'暂无信息'," +
"'Q074':'暂无信息'," +
"'Q075':'暂无信息'," +
"'Q056':'暂无信息'" +
"}";
ViewBag.fang = userid().Community;
return Json(str.Replace('\'', '"'));
}
#endregion
}
else
{
return Json("未认证或认证未通过");
}
}
#endregion
$.ajax({
type: 'post',
url: '/Home/YearDay',
data: { "year": year, "month": month },
success: function (msg) {
msg = JSON.parse(msg)
console.log(msg) $("#Amount").text("¥" + msg.Q071);//应缴合计
$("#Rent").text("¥" + msg.Q062);//租金
$("#PropertyFee").text("¥" + msg.Q064);//管理费
$("#WaterRate").text("¥" + msg.Q066);//水费
$("#PoolwaterFee").text("¥" + msg.Q067);//月水电分摊
$("#LightingElectricity").text("¥" + msg.Q065);//月电费
$("#MaintenanceCost").text("¥" + msg.Q068);//维修费
$("#OverdueFine").text("¥" + msg.Q069);//滞纳金 $("#TCLElectricity").text(msg.Q063);//是否含税
$("#PublicElectricity").text(msg.Q072);//应缴时间
$("#AirOvertime").text("¥" + msg.Q070);//总合计
}
})