本文摘自PHP中文网,作者巴扎黑,侵删。
这篇文章主要介绍了详解mvc使用JsonResult返回Json数据,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。controller 中定义以下方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public JsonResult UpdateSingle( int id, string actionName, string actionValue)
{
var res = new JsonResult();
var list = ( from a in db.Articles
select new
{
name = a.ArtTitle,
yy = a.ArtPublishTime
}).Take(5);
res.Data = list;
var name = "小华" ;
var age = "12" ;
var name1 = "小华" ;
var age1 = "12" ;
res.Data = new object [] { new { name, age }, new { name1, age1 } };
var person = new { Name = "小明" , Age = 22, Sex = "男" };
res.Data = person;
res.Data = "这是个字符串" ;
res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return res;
}
|
页面调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <a href= "javascript:void(0);" onclick= "javascript:upclick(this);" >Click Me</a>
<script type= "text/javascript" >
function upclick(o) {
var obj = $(o);
alert(obj);
$.ajax({
url: "/Articles/UpdateSingle?ran=" + Math.random(),
type: "GET" ,
dataType: "json" ,
data: { id: obj.attr( "id" ), actionName: obj.attr( "actionName" ), actionValue: obj.attr( "actionValue" ) },
success: function (data) {
$(o).html(data[0].name);
obj.attr( "actionValue" , data[0].result);
}
})
}
</script>
|
以上是在mvc中使用,在webform中怎么使用呢?
阅读剩余部分
相关阅读 >>
asp.net关于cookie跨域的问题
asp.net控制文件上传的大小方法(超简单)_实用技巧
asp.net core应用程序在linux上部署的图文详解
属性值反序列化失败怎么解决?
详细介绍《asp.net》数据绑定――gridview
asp.net禁用页面缓存的方法总结
asp.net完成文件上传的代码教程
用asp.net mvc部分视图渲染html的实例教程
asp.net中怎样用mvc5的miniprofiler对mvc进行性能监控
asp.net mvc 对输入的字符串字段做trim处理的方法_实用技巧
更多相关阅读请进入《javascript》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » 使用asp.net mvc使用JsonResult返回Json数据实例详解