接着上一篇博客,这次来完成多条数据的提交,我采用的是for循环,从session中获取到市直单位的id,从界面获取到提交指标的年份,指标类型,还有多条指标名称采用for循环的方式依次将他们放到数组中,因为我的数据要采用json格式,为了符合json的要求我需要将其放到对象中,然后使用stringify()方法,将其解析为字符串的形式传到一般处理程序中,接下来只要对解析出的数据依次写入数据库就O了,下面是具体的操作
界面还是上一篇博客中看到的
js部分
function saveTarget() {
document.getElementById("test").value = "save";
//设定表示为后台调不同方法数据提供接口
//document.getElementById("test").value = "add";
//1.将id=txtName的value值存放到(UnitQuantifyTarget表)中的名称字段
var txtNode = document.getElementsByName("txtName");
//找到所有指标名称的个数
var txtcount = parseInt($("input[name = ‘txtName‘]").length);
//2.将id=targetstyle的value值存放到(UnitQuantifyTarget表)中的;类别字段
var styleNode = document.getElementById("targetstyle");
var checkValue = styleNode.options[styleNode.selectedIndex].text;
//3.从服务器获取当前的年份,将其存放到(UnitQuantifyTarget表)中的年份字段
var yearNode = document.getElementById("year");
var yearValue = yearNode.options[yearNode.selectedIndex].text;
//用于向一般处理程序传值
var test = document.getElementById("test").value;
//实例化一个对象 ,用于存放test值
var data1 = new Object();
data1.test = test;
//实例化一个数组用于存放从前台获取的值对象
var array = new Array();
//for循环,用于将多条数据同时传入到数组中,以便传到一般处理程序
for (i = 0; i < parseInt($(txtNode).length) ; i++) {
var obj = new Object();
obj.txtname = txtNode[i].value;
//如果指标名称不为空的话就将其加入到数组中
if (obj.txtname != null && obj.txtname != "") {
obj.stylenode = checkValue;//指标类型
obj.year = yearValue;//时间
obj.ifrecord = "未录入";//录入状态
obj.unitId = "1";//市直单位id,应该是从session中获取,这里作为例子就直接拿一个数来代替了
array.push(obj);
}
}
data1.array = array;
//解析字符串
var nid = JSON.stringify(data1);
$.post("SubmitTargetJob.ashx", { action: "post", jsonArray: "" + nid + "" }, function (data) {
if (data == "false") {
alert("指标已存在,请核对信息重新添加!", "添加失败");
} else {
alert("添加成功!", "提示");
window.location.reload();//重新加载页面
}
}, "text");
}
一般处理程序
public void saveTarget(HttpContext context,JArray objget) {
List<UnitQuantifyTargetEntity> list = new List<UnitQuantifyTargetEntity>();
for (int i = 0; i < objget.Count; i++)
{
UnitQuantifyTargetEntity unitQuantifyTargetInfoEntity = new UnitQuantifyTargetEntity();
JObject objectget = (JObject)objget[i];
//解析从界面中传来的数据,注意[]中的内容一定要与界面传值时的名字一模一样。
string txtname = objectget["txtname"].ToString().Trim();
string stylenode = objectget["stylenode"].ToString().Trim();
string yearValue = objectget["year"].ToString().Trim();
string ifrecord = objectget["ifrecord"].ToString().Trim();
string unitId = objectget["unitId"].ToString().Trim();
//时间戳
PublicBLL publicBll = new PublicBLL();
string timestamp = publicBll.GetTime();
//每循环一次将从界面获取的数据放到实体中以便D层使用
unitQuantifyTargetInfoEntity.Name = txtname;
unitQuantifyTargetInfoEntity.Type = stylenode;
unitQuantifyTargetInfoEntity.Weight = null;
unitQuantifyTargetInfoEntity.ResponsibilityUnit1 = null;
unitQuantifyTargetInfoEntity.ResponsibilityUnit2 = null;
unitQuantifyTargetInfoEntity.ResponsibilityUnit3 = null;
unitQuantifyTargetInfoEntity.Timestamp = timestamp;
unitQuantifyTargetInfoEntity.YearTime = yearValue;
unitQuantifyTargetInfoEntity.CityUnitId = unitId;
unitQuantifyTargetInfoEntity.IfRecord = ifrecord;
unitQuantifyTargetInfoEntity.TargetId = null;
unitQuantifyTargetInfoEntity.other3 = null;
unitQuantifyTargetInfoEntity.other4 = null;
unitQuantifyTargetInfoEntity.other5 = null;
//没循环一次将实体中的内容放到list中,循环结束后,所有的内容就都到list中啦,到时候用就好啦
list.Add(unitQuantifyTargetInfoEntity);
}
if (unitQuantifyTargetBll.Add(list))//记得将list传过去
{
//调用B层的方法,如果成功了就将“true”返回到界面,界面会根据返回值继续后边的工作
context.Response.Write("true");
}
else
{
context.Response.Write("false");
}
}
B层
/// <summary>
/// 增加n条数据
/// </summary>
public bool Add(List<UnitQuantifyTargetEntity> list)
{
return dal.Add(list);
}
D层
/// <summary>
/// 增加n条数据
/// </summary>
public bool Add(List<UnitQuantifyTargetEntity> list)
{
bool flag = false;
foreach (UnitQuantifyTargetEntity model in list)
{
if (ExistsTarget(model) == false)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into T_UnitQuantifyTarget(");
strSql.Append("Name,Type,Weight,ResponsibilityUnit1,ResponsibilityUnit2,ResponsibilityUnit3,YearTime,CityUnitId,Timestamp,IfRecord,TargetId,other3,other4,other5)");
strSql.Append(" values (");
strSql.Append("@Name,@Type,@Weight,@ResponsibilityUnit1,@ResponsibilityUnit2,@ResponsibilityUnit3,@YearTime,@CityUnitId,@Timestamp,@IfRecord,@TargetId,@other3,@other4,@other5)");
SqlParameter[] parameters = {
//new SqlParameter("@Id", SqlDbType.VarChar,50),
new SqlParameter("@Name", SqlDbType.VarChar,100),
new SqlParameter("@Type", SqlDbType.VarChar,50),
new SqlParameter("@Weight", SqlDbType.VarChar,50),
new SqlParameter("@ResponsibilityUnit1", SqlDbType.VarChar,100),
new SqlParameter("@ResponsibilityUnit2", SqlDbType.VarChar,100),
new SqlParameter("@ResponsibilityUnit3", SqlDbType.VarChar,100),
new SqlParameter("@YearTime", SqlDbType.VarChar,50),
new SqlParameter("@CityUnitId", SqlDbType.VarChar,50),
new SqlParameter("@Timestamp", SqlDbType.VarChar,50),
new SqlParameter("@IfRecord", SqlDbType.VarChar,50),
new SqlParameter("@TargetId", SqlDbType.VarChar,50),
new SqlParameter("@other3", SqlDbType.VarChar,50),
new SqlParameter("@other4", SqlDbType.VarChar,50),
new SqlParameter("@other5", SqlDbType.VarChar,50)
};
//parameters[0].Value = model.Id;
parameters[0].Value = model.Name;
parameters[1].Value = model.Type;
parameters[2].Value = model.Weight;
parameters[3].Value = model.ResponsibilityUnit1;
parameters[4].Value = model.ResponsibilityUnit2;
parameters[5].Value = model.ResponsibilityUnit3;
parameters[6].Value = model.YearTime;
parameters[7].Value = model.CityUnitId;
parameters[8].Value = model.Timestamp;
parameters[9].Value = model.IfRecord;
parameters[10].Value = model.TargetId;
parameters[11].Value = model.other3;
parameters[12].Value = model.other4;
parameters[13].Value = model.other5;
int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
if (rows > 0)
{
flag = true;
}
else
{
flag = false;
}
}else{
flag = false;
}
}
return flag;
}
这就是整个多数据提交的过程,相当于你要将多条数据打包传到一般处理程序中,然后通过解析等过程获取数据,通过for循环依次将获取的数据放到实体中,每循环一次就将数据放到list中,循环结束之后,list中有你需要的所有数据,最后依次将list中的内容保存就可以了.
这三篇博客相当于我做的难点部分,做这个难点的主要原因是我们想让用户用着更方便,为了在任何位置都能添加一行,但是在实际项目中只考虑这一点是不够的,考虑多方便的因素我使用了easy_ui的datagrid控件中的Cell Editing in DataGrid控件,这个控件直接能编辑,并对选择的多条数据进行提交,挺方便的,随后我会把使用easy_ui控件做的上传到csdn,供大家参考.