.Net mvc 根据前台参数动态绑定对象
业务需求:根据前台界面的参数,动态绑定对象
<param name="colNames">属性名拼接字符串</param>
<param name="data">数据json字符串(前台数组形式)</param>
public ActionResult BindObject(string colNames,string data)
{
var jsonData = JsonConvert.DeserializeObject<dynamic>(data);
if (colNames == null)
{
return RedirectToAction("Index", new { area = "Assessment" });
}
string[] colNamesArray = colNames.Split(',');
List<ExpandoObject> list = new List<ExpandoObject>();
foreach (var item in jsonData)
{
dynamic expandoObject = new ExpandoObject();
foreach (var colName in colNamesArray)
{
(expandoObject as ICollection<KeyValuePair<string, object>>).Add(new KeyValuePair<string, object>(colName, item[colName]));
}
list.Add(expandoObject);
}
return View();
}
-----------------------------------------------------------------------------------------
前台js代码:
//收集列头(属性名)数据
var array = new Array();
var table = $('#tb-data')[0];
for (var i = 0; i < 1; i++) {
for (var j = 0; j < table.rows[0].cells.length; j++) {
var colName = table.rows[0].cells[j].innerText;
array.push(colName);
}
}
//收集数据
var dataArray = new Array();
for (var i = 1; i < table.rows.length; i++) {
var value = {};
for (var j = 0; j < table.rows[i].cells.length; j++) {
if (table.rows[i].cells[j].innerText == undefined || table.rows[i].cells[j].innerText == "") {
value[array[j]] = "";
} else {
value[array[j]] = table.rows[i].cells[j].innerText;
}
}
dataArray.push(value);
}
var postUrl = '/Accounting/AllEvaluate/Export';//提交地址
var postData = JSON.stringify(dataArray);//第一个数据
var ExportForm = document.createElement("FORM");
document.body.appendChild(ExportForm);
ExportForm.method = "POST";
var newElement = document.createElement("input");
newElement.setAttribute("name", "data");
newElement.setAttribute("type", "hidden");
ExportForm.appendChild(newElement);
var newElement2 = document.createElement("input");
newElement2.setAttribute("name", "colNames");
newElement2.setAttribute("type", "hidden");
ExportForm.appendChild(newElement2);
newElement2.value = array;
newElement.value = postData;
ExportForm.action = postUrl;
ExportForm.submit();
.Net mvc 根据前台参数动态绑定对象的更多相关文章
- 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface
后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...
- springMVC通过ajax传递参数list对象或传递数组对象到后台
springMVC通过ajax传递参数list对象或传递数组对象到后台 环境: 前台传递参数到后台 前台使用ajax 后台使用springMVC 传递的参数是N多个对象 JSON对象和JSON字符串 ...
- Spring MVC url提交参数和获取参数
[转载:http://blog.csdn.net/mahoking] 普通URL提交参数 该格式url为:url.do?param1=mahc¶m2=8888.00 需 ...
- spring mvc获取路径参数的几种方式 - 浅夏的个人空间 - 开源中国社区
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- MVC接收列表参数
ASP.NET MVC 表单参数如果有列表时要怎么写呢. 虽然很久不用MVC了,但几乎每次遇到一次就要研究一下.然后又忘了. 其实也明白这是未完全弄清楚表单参数的传递形式,如果明白了,就知道MVC为 ...
- django----orm查询优化 MTV与MVC模型 choice参数 ajax serializers
目录 orm查询优化 only defer select_related 与 prefetch_related MTV 与 MVC 模型 choice参数 Ajax 前端代码 后端代码 前后端传输数据 ...
- mvc中多参数URL会很长,首次加载不传参数让url很短,路由规则实现方法[bubuko.com]
如要实现列表中地址全路径“bubuko-11-2.html”,在首次进入时,使用短路径“bubuko.html”,只有再次href后才显示全路径“bubuko-11-2.html”,下面使用路由规则来 ...
- 使用自定义setTimeout和setInterval使之可以传递参数和对象参数
转载自http://www.jb51.net/article/17859.htm /****************************************************** // ...
- 修改 window.setTimeout,使之可以传递参数和对象参数
/* 功能:修改 window.setTimeout,使之可以传递参数和对象参数 使用方法: setTimeout(回调函数,时间,参数1,,参数n) */ var _setTimeout=setTi ...
随机推荐
- 基于 Cmd MarkDown 的 markdown 语法学习
首先我要打一个属于干货的广告:CmdMarkDown 是非常好用的markdown编辑器软件,支持全平台,由作业部落出品,分为客户端与WEB端两种使用场景. 本篇博客学习的markdown语法都是基于 ...
- 表格与ckeckbox的全选与单选
先看看下面的效果: 用户点击头的checkbox时,所有表格数据行的checkbox全选或反选. 当数据行某一行没有选中时,头checkbox去选.当所有数据行的checkbox全选时,头的check ...
- 【WPF】ChartControl的使用
一.前言 本月正好做一些关于工程4D,5D的界面展示,正好要用到Dev控件中的ChartControl控件,也就是图表控件. 折腾了两星期完成了一个比较能说的过去的界面吧.(领导要求高,可 ...
- WPF多源绑定
将控件绑定到多个数据源,ListBox绑定到一个集合,其中每一项绑定到集合中对象的两个属性,并对绑定进行了格式化. <ListBox ItemsSource="{StaticResou ...
- hibernate 中根据id删除一条记录的语句
qid name like content 1 A 1 the first text 2 B 2 the Second text 1 C 3 the Third text 如上表所示,当我们需要某个q ...
- JAVA使用JDBC技术操作SqlServer数据库
JDBC(JavaData Base Connectivity,Java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组 ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 浅谈Angular的 $q, defer, promise
浅谈Angular的 $q, defer, promise 时间 2016-01-13 00:28:00 博客园-原创精华区 原文 http://www.cnblogs.com/big-snow/ ...
- 地图中插入表格——ArcMap篇
在制作专题图的过程中,不但要有地理要素表示空间位置,经常还要在图的周围制作一些表格数据.这里对ArcMap中的插入方法进行总结. 方法一:插入对象 利用菜单中的"插入"-" ...
- LoadRunner免费公开课,惠普金牌讲师亲授
[开课时间]:9月13日 下午2:00—4:00(暂定)[活动费用]:免费[主办单位]:慧都学院[课程形式]:网络在线公开课 LoadRunner简介惠普软件测试解决方案LoadRunner测试实例答 ...