ASP.NET MVC:修改保存数据

今天讲的是最后一个基本操作,修改保存数据,分两个,一个数据回填,再之就是保存修改数据,在新增修改删除里,修改跟其它两个区别比较起来更要难上手一点点,多了一个数据回填。
以下是控制器部分
public ActionResult Updataemployee(PW_Employee Semployee)
{
ReturnJson returnJson = new ReturnJson();
try
{
//这里的判断是判断和数据库里其他的数据有没有冲突
int otherCount = (from tbemployee in myModels.PW_Employee
where tbemployee.employeeID != Semployee.employeeID &&
(tbemployee.employeeName == Semployee.employeeName.Trim()
|| tbemployee.telphone == Semployee.telphone.Trim())
select tbemployee).Count();
if (otherCount == 0)
{
//获取要修改的数据
PW_Employee dbemployee = (from tbemployee in myModels.PW_Employee
where tbemployee.employeeID == Semployee.employeeID
select tbemployee).Single();

                dbemployee.employeeName = Semployee.employeeName;
                dbemployee.employeeNum = Semployee.employeeNum;
                dbemployee.telphone = Semployee.telphone;
                dbemployee.address = Semployee.address;

                myModels.Entry(dbemployee).State = EntityState.Modified;
                if (myModels.SaveChanges() > 0)
                {
                    returnJson.State = true;
                    returnJson.Text = "修改成功!";
                }
                else
                {
                    returnJson.State = false;
                    returnJson.Text = "修改失败!";
                }
            }
            else
            {
                returnJson.State = false;
                returnJson.Text = "名字或电话号码重复!";
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            returnJson.State = false;
            returnJson.Text = "数据异常!";
        }
        return Json(returnJson, JsonRequestBehavior.AllowGet);
    }

这里还要再写一个查询,通过ID查询来整张表,返回单条数据,用来进行数据回填,这是我能记得的一个方法,老师好像说过还有另一种方法,不过我不记得了
public ActionResult EmployeeByID (int employeeID)
{
try{
PW_Employee list = (from tbemployee in myModels.PW_Employee where tbemployee.employeeID == employeeID select tbemployee).Single();
return Json(list, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
Console.WriteLine(e);
return Json("", JsonRequestBehavior.AllowGet);
}

    }

控制器部分的就写完了,接下来就是js部分
//保存修改
function saveUpdate() {
//获取input标签文本值
var employeeID = $("#employeeIDS").val();
var employeeNum = $("#employeeNumS").val();
var employeeName = $("#employeeNameS").val();
var telphone = $("#telphoneS").val();
var address = $("#addressS").val();
//判断
if (employeeID != ‘’ && employeeNum != ‘’&& employeeName != ‘’ && telphone != ‘’&& address != ‘’) {
//异步提交数据
$.post("/Main/Updataemployee",
{ employeeID:employeeID,
employeeNum: employeeNum,
employeeName: employeeName,
telphone: telphone,
address: address},
function (returnJson) {
if (returnJson.State == true) {
//关闭模态框
$("#modalEmployeeS").modal(‘hide’);
//刷新table
Avenger = layuiTable.reload(‘employee’);
}
layer.alert(returnJson.Text);
}, “json”);
} else {
//提示
layer.alert(‘请填写完整’, { title: ‘提示’, icon: 0 });
}}
写完保存修改的方法后再来写回填数据(两个顺序没有先后之分),
//打开修改模态框
function openUpdate(EmployeeID) {
//重置表单
$(’#formEmployeeS input[type=“reset”]’).click();
//回填数据
$.getJSON("/Main/EmployeeByID", { employeeID: EmployeeID }, function (data) { var obj = data;
var key, value, tagName, type, arr;
for (x in obj) {//循环json对象
key = x;
value = obj[x];
//更加form表单id 和 json对象中的key查找 表单控件
$("#formEmployeeS" + " [name=’" + key + “’],#formEmployeeS” + " [name=’" + key + “[]’]”).each(function () {
tagName = $(this)[0].tagName;
type = $(this).attr(‘type’);
if (tagName == ‘INPUT’) {
if (type == ‘radio’) {
$(this).attr(‘checked’, KaTeX parse error: Expected 'EOF', got '}' at position 57: … }̲ else if (type …(this).val() == arr[i]) {
$(this).attr(‘checked’, true);
break;
}
}
} catch (e) {
//单个
$(this).attr(‘checked’, value);
}
} else {
$(this).val(value);
}
} else if (tagName == ‘TEXTAREA’) {
KaTeX parse error: Expected 'EOF', got '}' at position 48: … }̲ else if (tagNa…(this).hasClass(“select2”)) {
$(this).val(value).trigger(“change”);
} else {
$(this).val(value);
}
}
});
}

            }, "json");
            //弹出模态框
            $("#modalEmployeeS").modal('show');
        }

就是利用循环来一条一条的把数据添加上去,回填数据也是这么一个原理
ASP.NET MVC:修改保存数据_第1张图片ASP.NET MVC:修改保存数据_第2张图片

你可能感兴趣的:(学习经历,菜鸟论)