[Layui]表单传值到后端的两种方式
Posted 厦门德仔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Layui]表单传值到后端的两种方式相关的知识,希望对你有一定的参考价值。
表单传值到后端的两种方式
表单传值
除通过url参数方式或者通过自定义对象方式传值外。
还有通过后端参数接收和同名对象传值两种方法
方法一:后端参数接收HttpContext.Request.Form[“xxx”];
//监听提交
form.on('submit(saveBtn)', function (data)
var formData = new FormData($("#sysUserForm")[0]);
var url = "http://localhost:8089/api/SysUser/AddSysUser";
$.ajax(
cache: true,
type: "post",
url: url,
async: false,
data: formData, // 你的formid
contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
success: function (msg)
var index = layer.alert(msg.resultMsg,
title: '上传成功'
, function ()
// 关闭弹出层
layer.close(index);
var iframeIndex = parent.layer.getFrameIndex(window.name);
parent.layer.close(iframeIndex);
//判断是否执行成功了
if (msg.resultMsg == "上传成功")
//执行表格重载
parent.reloadTable();
);
);
return false;
);
后端接收:
[HttpPost]
public JsonResult AddSysUser() //表单传值,后台接收参数
var Account = HttpContext.Request.Form["Account"];
var Password = HttpContext.Request.Form["Password"];
var Sex = int.Parse(HttpContext.Request.Form["Sex"]);
var Name = HttpContext.Request.Form["Name"];
var Email = HttpContext.Request.Form["Email"];
var Phone = HttpContext.Request.Form["Phone"];
var AdminType = int.Parse(HttpContext.Request.Form["AdminType"]);
var Status = int.Parse(HttpContext.Request.Form["Status"]);
var CreatedTime = System.DateTime.Now;
var CreatedUserId = int.Parse(HttpContext.Request.Form["UserId"]);
MD5Util md5 = new MD5Util();
SysUser sysUser = new SysUser()
Account = Account
,Password = md5.MD5Encrypt(Password)
,Sex = Sex
,Name=Name
, Email = Email
, Phone = Phone
, AdminType = AdminType
, Status = Status
,CreatedTime = CreatedTime
, CreatedUserId = CreatedUserId
;
misContext.SysUsers.Add(sysUser);
if (misContext.SaveChanges() > 0)
return new JsonResult(new isSuccess = true, resultMsg = "添加成功" );
else
return new JsonResult(new isSuccess = true, resultMsg = "添加失败" );
方法二 表单同名对象传值(表单name与类名一致)
//监听提交
form.on('submit(saveBtn)', function (data)
var url = "http://localhost:8089/api/SysUser/EditsysUser";
$.ajax(
url: url,
method: "POST",
data: JSON.stringify(data.field), //传对象:表单同名对象传值
dataType: "json",
contentType: 'application/json',
success: function (msg)
var index = layer.alert(msg.resultMsg,
title: '执行信息'
, function ()
// 关闭弹出层
layer.close(index);
var iframeIndex = parent.layer.getFrameIndex(window.name);
parent.layer.close(iframeIndex);
//判断是否执行成功了
if (msg.resultMsg == "修改成功")
//执行表格重载
parent.reloadTable();
);
);
return false;
);
后端接收
[HttpPost]
public JsonResult EditSysUser(SysUser sysUser) //表单同名对象传值
MD5Util md5 = new MD5Util();
if (sysUser != null)
SysUser sv = new SysUser()
Id = sysUser.Id
,Account = sysUser.Account
,Password = md5.MD5Encrypt(sysUser.Password)
,Name= sysUser.Name
,Sex = sysUser.Sex
,Email = sysUser.Email
,Phone = sysUser.Phone
,AdminType = sysUser.AdminType
,Status = sysUser.Status
,UpdatedTime=System.DateTime.Now
,UpdatedUserId = sysUser.UpdatedUserId
;//列Id是Key,声明Key属性列Code的值,及要修改的列Name的值
misContext.Attach(sv);//告诉EF Core开始跟踪person实体的更改,因为调用DbContext.Attach方法后,EF Core会将person实体的State值(可以通过testDBContext.Entry(person).State查看到)更改回EntityState.Unchanged,所以这里testDBContext.Attach(person)一定要放在下面一行testDBContext.Entry(person).Property(p => p.Name).IsModified = true的前面,否者后面的testDBContext.SaveChanges方法调用后,数据库不会被更新
misContext.Entry(sv).Property(p => p.Account).IsModified = true;//告诉EF Core实体person的Name属性已经更改。将testDBContext.Entry(person).Property(p => p.Name).IsModified设置为true后,也会将person实体的State值(可以通过testDBContext.Entry(person).State查看到)更改为EntityState.Modified,这样就保证了下面SaveChanges的时候会将person实体的Name属性值Update到数据库中。
misContext.Entry(sv).Property(p => p.Password).IsModified = true;
misContext.Entry(sv).Property(p => p.Sex).IsModified = true;
misContext.Entry(sv).Property(p => p.Name).IsModified = true;
misContext.Entry(sv).Property(p => p.Email).IsModified = true;
misContext.Entry(sv).Property(p => p.Phone).IsModified = true;
misContext.Entry(sv).Property(p => p.AdminType).IsModified = true;
misContext.Entry(sv).Property(p => p.Status).IsModified = true;
misContext.Entry(sv).Property(p => p.UpdatedUserId).IsModified = true;
if (misContext.SaveChanges() > 0)
return new JsonResult(new isSuccess = true, resultMsg = "修改成功" );
else
return new JsonResult(new isSuccess = false, resultMsg = "修改失败" );
else
return new JsonResult(new isSuccess = false, resultMsg = "修改失败" );
以上是关于[Layui]表单传值到后端的两种方式的主要内容,如果未能解决你的问题,请参考以下文章
Struts2中jsp前台传值到action后台的三种方式以及valueStack的使用