ASP.NET MVC Action向视图传值之匿名类型
程序员文章站
2022-07-05 08:29:56
本章主要和大家分享下在ASP.NET MVC中我们的Action如何向视图传递匿名类型的值。 ......
在使用asp.net mvc过程中想必大家都有遇到过一个问题就是我们的action如何向视图传递匿名类型的值呢,如果不做特殊处理则无法实现。
接下来我们来看一个示例:
在我们的控制中:
using system.collections.generic; using system.web.mvc; namespace tianya.dotnetshare.mvcdemo.controllers { public class democontroller : controller { // get: demo public actionresult index() { var liststu = new list<dynamic> { new { sno="1000", name = "张三", sex = "男", age =20 }, new { sno="1001", name = "李四", sex = "男", age =21 } }; var stu = new { sno = "1002", name = "钱七七", sex = "女", age = 20 }; viewbag.stu = stu; viewbag.liststu = liststu; return view(); } } }
在我们的视图中:
@{ layout = null; } <!doctype html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>index</title> </head> <body> <div> @foreach (var item in viewbag.liststu) { <p>学号:@(item.sno),姓名:@(item.name),性别:@(item.sex),年龄:@(item.age)。</p> } <p> 学号:@(viewbag.stu.sno),姓名:@(viewbag.stu.name),性别:@(viewbag.stu.sex),年龄:@(viewbag.stu.age)。 </p> </div> </body> </html>
然后将网站部署到我们的iis上面,接下来来访问一下我们的/demo/index
可以发现报错了,说明传值失败了,那么接下来我们来简单介绍下如何解决这个问题。
首先添加一个扩展方法帮助类:
using system; using system.collections.generic; using system.componentmodel; using system.dynamic; using system.linq; using system.xml; namespace tianya.dotnetshare.commtool { /// <summary> /// 扩展方法 /// </summary> public static class extentmethod { #region 匿名对象处理 #region 将对象[主要是匿名对象]转换为dynamic /// <summary> /// 将对象[主要是匿名对象]转换为dynamic /// </summary> public static dynamic todynamic(this object value) { idictionary<string, object> expando = new expandoobject(); var type = value.gettype(); var properties = typedescriptor.getproperties(type); foreach (propertydescriptor property in properties) { var val = property.getvalue(value); if (property.propertytype.fullname.startswith("<>f__anonymoustype")) { dynamic dval = val.todynamic(); expando.add(property.name, dval); } else { expando.add(property.name, val); } } return expando as expandoobject; } #endregion #region 将对象[主要是匿名对象]转换为list<dynamic> /// <summary> /// 将对象[主要是匿名对象]转换为list<dynamic> /// </summary> public static list<dynamic> todynamiclist(this ienumerable<dynamic> values) { var list = new list<dynamic>(); if (values != null) { if (values.any()) { list.addrange(values.select(v => ((object)v).todynamic())); } } return list; } #endregion #region 将匿名对象集合转换为xml /// <summary> /// 将匿名对象集合转换为xml /// </summary> public static xmldocument listobjerttoxml(this ienumerable<dynamic> values) { var xmldoc = new xmldocument(); var xmlelem = xmldoc.createelement("documentelement"); xmldoc.appendchild(xmlelem); if (values != null) { if (values.any()) { var node = xmldoc.selectsinglenode("documentelement"); foreach (var item in values) { var xmlrow = xmldoc.createelement("row"); objecttoxml(item, xmldoc, xmlrow); node.appendchild(xmlrow); } } } return xmldoc; } #endregion #region 将匿名对象填充xml节点 /// <summary> /// 将匿名对象填充xml节点 /// </summary> private static void objecttoxml(object value, xmldocument xmldoc, xmlelement xmlrow) { idictionary<string, object> expando = new expandoobject(); var type = value.gettype(); var properties = typedescriptor.getproperties(type); foreach (propertydescriptor property in properties) { var val = property.getvalue(value); xmlrow.clonenode(false); var xmltemp = xmldoc.createelement(property.name); xmltext xmltext; if (property.propertytype.fullname.startswith("<>f__anonymoustype")) { dynamic dval = val.todynamic(); xmltext = xmldoc.createtextnode(dval.objecttostring()); } else { xmltext = xmldoc.createtextnode(val.tostring()); } xmltemp.appendchild(xmltext); xmlrow.appendchild(xmltemp); } } #endregion #endregion } }
然后对我们的控制器做一些调整,增加调用我们的扩展方法:
using system.collections.generic; using system.web.mvc; using tianya.dotnetshare.commtool; namespace tianya.dotnetshare.mvcdemo.controllers { public class democontroller : controller { // get: demo public actionresult index() { var liststu = new list<dynamic> { new { sno="1000", name = "张三", sex = "男", age =20 }, new { sno="1001", name = "李四", sex = "男", age =21 } }; var stu = new { sno = "1002", name = "钱七七", sex = "女", age = 20 }; viewbag.stu = stu.todynamic(); viewbag.liststu = liststu.todynamiclist(); return view(); } } }
处理完后再来访问一下我们的/demo/index
可以看出正常显示了,说明我们的传值成功了。
至此,本章就介绍完了,如果你觉得这篇文章对你有所帮助请记得点赞哦,谢谢!!!
demo源码:
链接:https://pan.baidu.com/s/1_extkkybybfpbf5adt4asw 提取码:atem
版权声明:如有雷同纯属巧合,如有侵权请及时联系本人修改,谢谢!!!
上一篇: 出血的馒头