在传统网页和网站,软件,以及游戏反面等方面,都会提供一些修改不必要数据的功能,例如:QQ的称呢,
游戏的名称,购物网站上修改收货信息中的各种信息等等,即修改功能。
需要实现数据查询,数据回填,数据修改保存
Mvc控制器查询需要修改的数据
public ActionResult Accountparticulars(int AccountID)
{
try{
var Accountparticular = (from tbAccount in myModels.B_Account
join tbLi in myModels.S_LnitialAccount on
tbAccount.LnitialAccountID equals tbLi.LnitialAccountID
where tbAccount.AccountID == AccountID
select new
{
AccountID = tbAccount.AccountID,
LnitialAccountID = tbLi.LnitialAccountID,
AccountName = tbAccount.AccountName.Trim(),//账户名称
Accountbalance = tbAccount.Accountbalance,//账户余额
AcOpenbank = tbAccount.AcOpenbank.Trim(),//开户银行
AcNote = tbAccount.AcNote.Trim(),//备注
AcBankcardnumber = tbAccount.AcBankcardnumber,//银行卡号
LnitialBalance = tbLi.LnitialBalance,//期初余额
AcState = tbAccount.AcState,
}).Single();
return Json(Accountparticular, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
Console.WriteLine(e);
return Json(null, JsonRequestBehavior.AllowGet);
}
}
//页面JS
//修改数据回填
function AccountUpdate(AccountID) {
$('#UpdateAccountModal input[name = "reset" ]').click();
$.post("Accountparticulars", { AccountID: AccountID }, function (date) {
loadDatatoForm("UpdateAccountModal", date);
if (date.AcState == true) {
$("#StateSex").click();
}
if (date.AcState == false) {
$("#StateEmp").click();
}
});
$("#UpdateAccountModal").modal("show");
}
//修改保存
function UpdatePreserve() {
var ferfAccountName = $('#UpdateFromAccount [name = "AccountName"]').val();//账户名称
if (ferfAccountName != "") {
$("#UpdateFromAccount").ajaxSubmit(function (returnJsonddddd) {
if (returnJsonddddd.State == true) {
$("#UpdateAccountModal").modal("hide");
table.reload("AcountTitle");
layer.alert(returnJsonddddd.Text)
}
else {
layer.alert(returnJsonddddd.Text)
}
});
}
else {
layer.alert("账户名称不能为空?");
$("#UpthreeIconDivicon").css("display", "block");
}
}
修改的数据回填使用了一个封装的JS方法loadDatatoForm方法,主要是用于回填input文本框的值,是根据input标签中name的值回填对应的数据,name的值与数据库中的字段或你在查询中命名的字段相对应,该文本框就回填相对应的数据。
修改保存用的是from表单的提交中的ajaxSubmit,需要用的一个插件;
提交的路径放在:
Action指定提交路径、method指定提交的方式。
Mvc修改保存;
public ActionResult UpdateAccountone(B_Account BAccount, S_LnitialAccount SlnitialAccount)
{
ReturnJson returnJsonddddd = new ReturnJson();
try
{
//查重
var AccountCount = (from tbAccount in myModels.B_Account
where tbAccount.AccountID != BAccount.AccountID
&& tbAccount.LnitialAccountID == BAccount.LnitialAccountID
&& tbAccount.AccountName == BAccount.AccountName
select tbAccount).Count();
if (AccountCount == 0)
{
//查询期初数据
S_LnitialAccount LnitialAccount = (from tbLnitial in myModels.S_LnitialAccount
where tbLnitial.LnitialAccountID == SlnitialAccount.LnitialAccountID
select tbLnitial).Single();
//修改期初数据
LnitialAccount.LnitialAccountID = SlnitialAccount.LnitialAccountID;
LnitialAccount.LnitialBalance = SlnitialAccount.LnitialBalance;//期初余额
//修改保存
myModels.Entry(LnitialAccount).State = System.Data.Entity.EntityState.Modified;
// if (myModels.SaveChanges() > 0) 判断是否保存成功
if (myModels.SaveChanges() > 0)
{
//提取期初数据
var ID = LnitialAccount.LnitialAccountID;
var momo = LnitialAccount.LnitialBalance;//期初余额
//查询账户信息
B_Account listAvvount = (from tbAccount in myModels.B_Account
where tbAccount.AccountID == BAccount.AccountID
select tbAccount).Single();
//提取账户信息进行修改
listAvvount.AccountID = BAccount.AccountID;
listAvvount.LnitialAccountID = ID;
listAvvount.AccountName = BAccount.AccountName; // 账户名称
listAvvount.AcOpenbank = BAccount.AcOpenbank; //开户银行
listAvvount.AcBankcardnumber = BAccount.AcBankcardnumber;//银行卡号
listAvvount.AcNote = BAccount.AcNote;//备注
listAvvount.AcState = BAccount.AcState;//状态
//提取账户余额与期初余额比较
var mo = listAvvount.Accountbalance;
//判断账户余额是否为空,为空则赋值为0
if (mo == null)
{
mo = 0;
}
//判断账户余额与期初余额的关系
if (momo > mo )
{
listAvvount.Accountbalance = momo;
myModels.Entry(listAvvount).State = System.Data.Entity.EntityState.Modified;
if (myModels.SaveChanges() > 0)
{
returnJsonddddd.State = true;
returnJsonddddd.Text = "修改成功";
}
else
{
returnJsonddddd.State = false;
returnJsonddddd.Text = "修改失败";
}
}
else
{
myModels.Entry(listAvvount).State = System.Data.Entity.EntityState.Modified;
if (myModels.SaveChanges() > 0)
{
returnJsonddddd.State = true;
returnJsonddddd.Text = "修改成功";
}
else
{
returnJsonddddd.State = false;
returnJsonddddd.Text = "修改失败";
}
}
}
}
else
{
returnJsonddddd.State = false;
returnJsonddddd.Text = "该账户已经存在";
}
}
catch (Exception e)
{
Console.WriteLine(e);
returnJsonddddd.State = false;
returnJsonddddd.Text = "风蚀符文";
}
return Json(returnJsonddddd, JsonRequestBehavior.AllowGet);
}
在数据修改中,如果修改的数据中存在,表与表中的数据存在对比关系,则可能需要设定可修改条件。
例如:A表修改的主体表,B表是关系表,
如果A表中的某个字段与B表的某个字段存在对比关系,A表的字段必须符合某种条件,才能修改B表的字段,如果不符合条件,则只可修改A表的字段,不可修改B表的字段。