读书人

求教easyUI如何获取MVC后台生成的数据

发布时间: 2013-06-19 10:26:41 作者: rapoo

求教easyUI怎么获取MVC后台生成的数据
这是后台的数据


public ActionResult ResidentInfo()
{
StringBuilder JsonString = new StringBuilder();
JsonString.Append("{\"total\":10,\"rows\":");
JsonString.Append("[");
//for (int i = 0; i < rTB.Rows.Count; i++)
for (int i = 0; i < 10;i++ )
{
JsonString.Append("{");
JsonString.Append("\"residentID\":\"这是用
户ID\",\"ResidnetName\":\"这是名字\"");

JsonString.Append("}");
if(i<9)
JsonString.Append(",");
}
JsonString.Append("]}");


return Content(JsonString.ToString(),"Application/json");
}



这是前台代码:
    $('#MeteringTable').datagrid({
url: '/Query/ResidentInfo',
columns: [[
{ field: 'productid', title: '用户ID', width: 200 },
{ field: 'productname', title: '用户名', width: 200 }
]]
});

怎么都获取不到数据,请教该如何从后台控制中获取数据 MVC easyui ASP.NET DataGrid
[解决办法]
后台 control 这样写
public string ResidentInfo()
{
返回json串 注意这里拼接的格式,去easyui 官网上面看他json串的格式进行拼装,这里很容易拼错的。
}
不要写在你的默认的 action 里面 单独页面用ajax来请求一次, 因为默认的action 返回的是actionResult 是一个view





[解决办法]
JsonHelper.CreateJsonParameters 的方法体内容如下



/// <summary>
/// 将List中的数据转换成JSON格式
/// </summary>
/// <param name="dt">数据源list</param>
/// <param name="displayCount">是否输出数据总条数</param>
/// <param name="totalcount">JSON中显示的数据总条数</param>
/// <returns></returns>
public static string CreateJsonParameters<T>(List<T> list, bool displayCount, int totalcount)
{
if (list.Count == 0)
return "{\"rows\": 0,\"total\":0}";
StringBuilder JsonString = new StringBuilder();
if (list != null)
{
JsonString.Append("{ ");
JsonString.Append("\"rows\":[ ");
T _t = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] propertys = _t.GetType().GetProperties();
List<string> titleList = new List<string>();
titleList = propertys.Select(t => t.Name).ToList();
foreach (var item in list)
{
JsonString.Append("{ ");
int titleLength = 1;
foreach (var titleItem in titleList)
{
PropertyInfo pi = propertys.First(p => p.Name == titleItem);
var value = pi.GetValue(item, null);
JsonString.Append(CheckPropertyDataType(pi.PropertyType, titleItem, value));


if (titleLength == titleList.Count)
JsonString.Remove((JsonString.Length - 1), 1);
titleLength++;
}
JsonString.Append("}, ");
}
JsonString.Remove((JsonString.Length - 2), 1);
JsonString.Append("]");
if (displayCount)
JsonString.Append(",\"total\":" + totalcount);
}
JsonString.Append("}");
return JsonString.ToString().Replace("\n", "");
}

读书人网 >JavaScript

热点推荐