使用 Easy+ASP.NET MVC 接收json数据

屎涂行者 2017-03-20 11:53:19



<script src="../../JS/Script/JavaScript/jquery-1.8.2.min.js" type="text/javascript"></script>
<link href="../../EasyUI/css/default.css" rel="stylesheet" type="text/css" />
<link href="../../EasyUI/js/themes/default/easyui.css" rel="stylesheet" type="text/css" />
<link href="../../EasyUI/js/themes/icon.css" rel="stylesheet" type="text/css" />
<script src="../../EasyUI/js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="../../EasyUI/js/jQuery.easyui.js" type="text/javascript"></script>


<div id="dlg" class="easyui-dialog" title="策略规则" style="width:550px;height:300px;"
data-options="resizable:true,modal:true">
<table id="dg" class="easyui-datagrid" style="width:550px;height:300px;"
data-options="singleSelect:true,collapsible:true">
<thead>
<tr>
<th field="PostID" width="60">帖子ID</th>
<th field="UserID" width="100">用户ID</th>
<th field="PostTitle" width="150">标题</th>
<th field="offNo" width="50">是否正常</th>
<th field="PostDengjiID" width="50"">等级</th>
</tr>
</thead>
</table>
</div>

<script type="text/javascript">
$(function () {
alert("22222");
var url = "../ajax/DemoJson2.ashx";
$.post(url, function (res) {
alert(res);
var json = JSON.parse(res);
alert(json);
var datasource = { "total": total.toString, "rows": json };
$("#dg").datagrid('loadData', datasource);
$('#dlg').dialog('open');
})
});
</script>





ashx.cs代码

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int total = 0;
//获取该策略下的所有规则
List<Post> list = new List<Post>();

string sql = "Select PostID,PostTitle,PostDengJiID,OffNo,UserID From Post";
DataTable dt = DBHelper.GetDataTable(sql);
for (int i = 0; i < dt.Rows.Count; i++)
{
Post rule = new Post();
rule.PostID = Convert.ToInt32(dt.Rows[i]["PostID"].ToString());
rule.PostTitle = dt.Rows[i]["PostTitle"].ToString();
string ruleType =dt.Rows[i]["PostDengjiID"].ToString();
//规则类型
//1 普通
//2 精品
//3 置顶
//4 警告
switch (ruleType)
{
case "1":
rule.PostDengjiID = "普通";
break;
case "2":
rule.PostDengjiID = "精品";
break;
case "3":
rule.PostDengjiID = "置顶";
break;
case "4":
rule.PostDengjiID = "警告";
break;
}
string offno = dt.Rows[i]["offNo"].ToString();
switch (offno)
{
case "1" :
rule.offNo = "正常";
break;
case "0" :
rule.offNo = "不正常";
break;
}
rule.UserID =Convert.ToInt32(dt.Rows[i]["UserID"].ToString());
list.Add(rule);
}
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = jss.Serialize(list);
total = list.Count;
context.Response.Write(json);


//System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(list.GetType());
//using (MemoryStream ms = new MemoryStream())
//{
// serializer.WriteObject(ms, list);
// context.Response.Write(Encoding.UTF8.GetString(ms.ToArray()));
//}

}


返回的数据在JS 中能接收到正常的Json数据,但是在跟动态表头配合显示数据时就是显示不出,请问各位大神这是什么原因啊?是不是有什么文件没有引用到?还是代码写错什么的。找了好久没有找出问题







...全文
359 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
by_封爱 2017-03-20
  • 打赏
  • 举报
回复
1`你这东西跟mvc好像没什么关系... 2`构造json的问题..

var datasource = { "total": total.toString, "rows": json };
明显的 你连total参数都没有啊... 最起码的 你输出的时候应该是这样的..

var json=new{total=list.count,list=list};
write(Serialize(json));

var datasource = { "total": json.total "rows": json.list };
当然这只是基本的..
songzx66 2017-03-20
  • 打赏
  • 举报
回复
datagrid的参数给的不全,找不到对应的field。 建议仔细看一下文档 http://www.jeasyui.com/extension/datagridview.php
丰云 2017-03-20
  • 打赏
  • 举报
回复
顺便说下,jquery的ajax方法,是可以直接拿到json格式的数据的,完全不需要JSON.parse(res)这样多此一举的。。。。。
丰云 2017-03-20
  • 打赏
  • 举报
回复
既然js取到了json数据,你贴后端那么多代码有毛用。。。。。 你的取到的数据没有绑定到datagrid,你应该检查你的绑定方法!!! 你能查看是否取到了json数据,为何不能继续调试下去呢???? 真心搞不懂。。。。
这个文档是我自己原创制作的,在别的网上肯定是没有的。 而且做得非常好看,和非常准确。 如果下载的人多,将会把中英文对照的版本也上传。 Knockout是一个以数据模型(data model)为基础的能够帮助你创建富文本,响应显示和编辑用户界面的JavaScript类库。任何时候如果你的UI需要自动更新(比如:更新依赖于用户的行为或者外部数据源的改变),KO能够很简单的帮你实现并且很容易维护。 重要特性: 优雅的依赖追踪 - 不管任何时候你的数据模型更新,都会自动更新相应的内容。 Elegant dependency tracking - automatically updates the right parts of your UI whenever your data model changes. 声明式绑定 - 浅显易懂的方式将你的用户界面指定部分关联到你的数据模型上。 Declarative bindings - a simple and obvious way to connect parts of your UI to your data model. You can construct a complex dynamic UIs easily using arbitrarily nested binding contexts. 轻易可扩展 - 几行代码就可以实现自定义行为作为新的声明式绑定。 Trivially extensible - implement custom behaviors as new declarative bindings for easy reuse in just a few lines of code. 额外的好处: 纯JavaScript类库 – 兼容任何服务器端和客户端技术 Pure JavaScript library - works with any server or client-side technology 可添加到Web程序最上部 – 不需要大的架构改变 Can be added on top of your existing web application - without requiring major architectural changes 简洁的 - Gzip之前大约25kb Compact - around 13kb after gzipping 兼容任何主流浏览器 - (IE 6+、Firefox 2+、Chrome、Safari、其它) Works on any mainstream browser - (IE 6+, Firefox 2+, Chrome, Safari, others) 采用行为驱动开发 - 意味着在新的浏览器和平台上可以很容易通过验证。 Comprehensive suite of specifications - (developed BDD-style) means its correct functioning can easily be verified on new browsers and platforms 开发人员如果用过Silverlight或者WPF可能会知道KO是MVVM模式的一个例子。如果熟悉 Ruby on Rails 或其它MVC技术可能会发现它是一个带有声明式语法的MVC实时form。换句话说,你可以把KO当成通过编辑JSON数据来制作UI用户界面的一种方式… 不管它为你做什么 Developers familiar with Ruby on Rails, ASP.NET MVC, or other MV* technologies may see MVVM as a real-time form of MVC with declarative syntax. In another sense, you can think of KO as a general way to make UIs for editing JSON data… whatever works for you :) OK, 如何使用它? 简单来说:声明你的数据作为一个JavaScript 模型对象(model object),然后将DOM 元素或者模板(templates)绑定到它上面. The quickest and most fun way to get started is by working through the interactive tutorials. Once you’ve got to grips with the basics, explore the live examples and then have a go with it in your own project. KO和jQuery (或Prototype等)是竞争关系还是能一起使用? 所有人都喜欢jQuery! 它是一个在页面里操作元素和事件的框架,非常出色并且易使用,在DOM操作上肯定使用jQuery,KO解决不同的问题。 Everyone loves jQuery! It’s an outstanding replacement for the clunky, inconsistent DOM API we had to put up with in the past. jQuery is an excellent low-level way to manipulate elements and event handlers in a web page. I certainly still use jQuery for low-level DOM manipulation. KO solves a different problem. 如果页面要求复杂,仅仅使用jQuery需要花费更多的代码。 例如:一个表格里显示一个列表,然后统计列表的数量,Add按钮在数据行TR小于5调的时候启用,否则就禁用。jQuery 没有基本的数据模型的概念,所以需要获取数据的数量(从table/div或者专门定义的CSS class),如果需要在某些SPAN里显示数据的数量,当添加新数据的时候,你还要记得更新这个SPAN的text。当然,你还要判断当总数>=5条的时候禁用Add按钮。 然后,如果还要实现Delete功能的时候,你不得不指出哪一个DOM元素被点击以后需要改变。 As soon as your UI gets nontrivial and has a few overlapping behaviors, things can get tricky and expensive to maintain if you only use jQuery. Consider an example: you’re displaying a list of items, stating the number of items in that list, and want to enable an ‘Add’ button only when there are fewer than 5 items. jQuery doesn’t have a concept of an underlying data model, so to get the number of items you have to infer it from the number of TRs in a table or the number of DIVs with a certain CSS class. Maybe the number of items is displayed in some SPAN, and you have to remember to update that SPAN’s text when the user adds an item. You also must remember to disable the ‘Add’ button when the number of TRs is 5. Later, you’re asked also to implement a ‘Delete’ button and you have to figure out which DOM elements to change whenever it’s clicked. Knockout的实现有何不同? 使用KO非常简单。将你的数据描绘成一个JavaScript数组对象myItems,然后使用模板(template)转化这个数组到表格里(或者一组DIV)。不管什么时候数组改变, UI界面也会响应改变(不用指出如何插入新行或在哪里插入),剩余的工作就是同步了。例如:你可以声明绑定如下一个SPAN显示数据数量(可以放在页面的任何地方,不一定非要在template里): It’s much easier with KO. It lets you scale up in complexity without fear of introducing inconsistencies. Just represent your items as a JavaScript array, and then use a foreach binding to transform this array into a TABLE or set of DIVs. Whenever the array changes, the UI changes to match (you don’t have to figure out how to inject new TRs or where to inject them). The rest of the UI stays in sync. For example, you can declaratively bind a SPAN to display the number of items as follows: There are items就是这些!你不需要写代码去更新它,它的更新依赖于数组myItems的改变。同样, Add按钮的启用和禁用依赖于数组 myItems 的长度,如下: That’s it! You don’t have to write code to update it; it updates on its own when the myItems array changes. Similarly, to make the ‘Add’ button enable or disable depending on the number of items, just write: 之后,如果你要实现Delete功能,不必指出如何操作UI元素,只需要修改数据模型就可以了。 Later, when you’re asked to implement the ‘Delete’ functionality, you don’t have to figure out what bits of the UI it has to interact with; you just make it alter the underlying data model. 总结:KO没有和jQuery或类似的DOM 操作API对抗竞争。KO提供了一个关联数据模型和用户界面的高级功能。KO本身不依赖jQuery,但是你可以一起同时使用jQuery, 生动平缓的UI改变需要真正使用jQuery。

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧