.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 ...
随机推荐
- CentOS系统MySQL双机热备配置
1 概述 在集成项目中需要应对不同环境下的安装配置,主流操作系统大致可以分为三种:Linux.Windows以及UNIX.其中Linux备受青睐的主要原因有两个: 首先,Linux作为自由软件有两个 ...
- [Winform] DataGridView 总结(FAQ)
Q1. 如何使单元格不可编辑? A:设置 ReadOnly 属性,可以设置的对象包括 DataGridViewRow(行).DataGridViewColumn(列).DataGridViewCel ...
- Yii2.x 互斥锁Mutex-类图
- JAVA 读写Excel
ExcelUtil.java package pers.kangxu.datautils.utils; import java.io.File; import java.io.FileInputStr ...
- DevOps的基本原则与介绍
DevOps的基本原则与介绍 DevOps这个术语是developer与operations的合并简写.实现还有QA.DevOps描述与精简软件交付流程,在今天已经开始广泛的使用.强调从生 ...
- java多线程同步,等待,唤醒
notify().notifyAll().wait()属于java.lang.Object,java.lang.Thread也是Object,自然也有上述方法: sleep().interrupt() ...
- 一步步实现ABAP后台导入EXCEL到数据库【1】
在SAP的应用当中,导入.导出EXCEL文件的情况是一个常见的需求,有时候用户需要将大量数据定期导入到SAP的数据库中.这种情况下,使用导入程序在前台导入可能要花费不少的时间,如果能安排导入程序为后台 ...
- iOS编码规范
The official raywenderlich.com Objective-C style guide. This style guide outlines the coding con ...
- hexo+next博客添加搜索
1.为什么添加algolia搜索 第一当然是可以方便的查找所需文章,第二点就是之前常用的swiftype插件不再免费.我的个人博客是这个月初搭建完成的,这时候swiftype已经不再免费,而且只开放企 ...
- javascript函数的几种写法集合
1.常规写法 function fnName(){ console.log("常规写法"); } 2.匿名函数,函数保存到变量里 var myfn = function(){ co ...