admin 管理员组文章数量: 887021
API ,批量添加
添加引用:cors
using system.web.http.cors
API添加这句话: [EnableCors("*", "*", "*")]
api的控制器添加: config.EnableCors();///自己写的
///批量添加前台
@{
Layout = null;
}
@model List< model. stuMM>
<!DOCTYPE html>
<html>
<head>
<input id="moreadd" οnclick="add()" class="btn btn-primary" type="button" value="批量添加" />
<input id="save" οnclick="saveadd()" type="button" class="btn btn-warning" value="批量保存" />
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style>
td{
width:10px;
}
</style>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
</head>
<body>
<div>
<table class="table table-bordered">
<tr>
<td >编号</td>
<td >姓名</td>
<td >学号</td>
<td >年龄</td>
</tr>
<tbody id="tbody1">
</tbody>
</table>
</div>
</body>
</html>
<script>
show();
//绑定数据
function show()
{
debugger;
$.ajax({
url: "/show/getdate",
type: "get",
dataType: "json",
success: function (data) {
debugger;
var str = '';
for (var i = 0; i < data.length; i++) {
str+=" <tr>";
str+="<td>"+data[i].id+"</td>";
str+="<td>"+data[i].StuNamer+"</td>";
str+="<td>"+data[i].studyNumbe+"</td>";
str += "<td>" + data[i].Age + "</td>";
str += "<td> <input id='del' type=\"button\" class=\"btn btn-primary\" οnclick=\"del("+data[i].id+")\" value=\"删除\" /></td>";
str += "</tr>";
}
$("#tbody1").html(str);
}
})
}
///删除
function del(i)
{
location.href = '/show/del?id='+i;
}
//添加
function add()
{
debugger;
var ss = Date.now();
var str = "";
str += " <tr class=\"ss\" id='" + ss + "'>";
debugger;
str += "<td><input type=\"checkbox\" checked='checked' οnclick='buxuan(" + ss + ")' name=" + ss + " /></td>";
str+="<td><input id=\"Text2\" type=\"text\" /></td>";
str+="<td><input id=\"Text3\" type=\"text\" /></td>";
str += "<td><input id=\"Text4\" type=\"text\" /></td>";
str += "<td><input type=\"button\" οnclick=\"dels(" +ss + ")\" value=\"删除行\" /></td>";
str+="</tr>";
$("#tbody1").append(str);
// alert(str);
}
//批量保存
function saveadd()
{
debugger;
var arry = [];//建立数组
$(".ss").each(function () { //循环
if($(this).children().eq(0).children().first().prop("checked")==true) //记住了true
{
// alert($(this).children().eq(0).children().first().attr("checked"));
debugger;
var ss = $(this).children().eq(1).children().first().val();
var sss = $(this).children().eq(2).children().first().val();
var ssss = $(this).children().eq(3).children().first().val();
//StuNamer n
//studyNumbe
//Age int
arry.push(JSON.stringify({ StuNamer: ss, studyNumbe: sss, Age: ssss }));
}
//console.log(arry);
})
debugger;
$.ajax({
url: "/show/add",
type: "post",
traditional: true,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
dataType: "json",
data: { arry: arry },
success: function (data) {
if(data>0)
{
alert("添加成功!");
show();
}
}
})
}
//不选
function buxuan(id)
{
debugger;
// alert( $(id).siblings().eq(0));
// $("input[name='" + id + "']").prop("checked", "");
// alert( $("input[name='" + id + "']").prop("checked"));
}
//删除行
function dels(i)
{
debugger;
$("#" + i).remove();
}
</script>
///批量添加后台
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Data;
using model;
namespace _11yue16rikao.Controllers
{
public class showController : Controller
{
// GET: show
public ActionResult Index()
{
stuMM MM = new stuMM();
ViewBag.ms = MM;
var data = new DAL.stuDAL().getdata();
return View(data);
}
/// <summary>
/// 获取数据
/// </summary>
/// <returns></returns>
public string getdate()
{
var data = new DAL.stuDAL().getdata();
var data2 = JsonConvert.SerializeObject(data);
return data2;
}
/// <summary>
/// 分页
/// </summary>
/// <returns></returns>
public void asas()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Students;Integrated Security=True");
SqlCommand cmd = new SqlCommand("proc_find_stu", con);
cmd.CommandType = CommandType.StoredProcedure;//指定执行的是存储过程
SqlParameter startId = new SqlParameter("@startId",1);
SqlParameter endId = new SqlParameter("@endId", 6);
SqlParameter outID = new SqlParameter("@outID",SqlDbType.Int);
outID.Direction = ParameterDirection.Output;//指定是输出参数
cmd.Parameters.Add(startId);
cmd.Parameters.Add(endId);
cmd.Parameters.Add(outID);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable tb = new DataTable();
da.Fill(tb);
var i = outID.Value; //获取id的值,就是条数
}
[HttpPost]
public ActionResult add(string[] arry) //字符数组
{
var str = " insert into student values ";
// var definition = new { StuNamer = "", studyNumbe = 0, Age = "" };//匿名类
stuMM mm = new stuMM(); //实例化stuMM类
var data = arry;
var ass = "";
for (int i = 0; i < data.Length; i++)
{
ass = data[i];//获取当前行的字符串
var person1 = JsonConvert.DeserializeAnonymousType(ass, mm);//把json转换为mm类
str += "( '"+person1.StuNamer+"','"+person1.studyNumbe+ "', '" + person1.Age + "') ,";
}
//var ss = str.Replace('}',' ') ; //把最后的}替换为空
//int chu = ss.LastIndexOf(',');
//var sss = ss.Substring(0,chu-1);
//var sql= sss.Replace('"','\'');
int chu = str.LastIndexOf(',');
var sss = str.Substring(0,chu-1);
int cg = new DAL.stuDAL().add(sss);
return Content(cg.ToString());
}
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult del(int id)
{
int i = new DAL.stuDAL().del(id);
return Content("<script>alert('删除成功');location.href='/show/Index'</script>");
}
}
}
、
转载于:.html
版权声明:本文标题:API ,批量添加 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1688033974h171269.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论