使用ajax进行新增
<script>
$(function () {
$("#Add").click(function () {
$.ajax({
type: "Post",
url: "/UserLogin/AddUser",
data: {
Name: $("#txtName").val(),
Age: $("#txtAge").val(),
Emal: $("#txtEmal").val(),
Phone: $("#txtPhone").val()
},
success: function (st) {
if (st.status == 200) {
alert("添加成功!");
window.location.href = "/UserLogin/Users";
}
else if (st.status == 401) {
alert("姓名不能为空!");
return;
}
else if (st.status == 402) {
alert("年龄不能为空!");
return;
}
else {
alert("添加失败!");
}
}
})
})
})
</script>
[HttpPost]
public ActionResult AddUser(CS_Users cs_user)
{
try
{
if (ModelState.IsValid)
{
using (MedicalInformationDataBaseEntitiesAAA tv = new MedicalInformationDataBaseEntitiesAAA())
{
CS_Users bd = new CS_Users();
if(cs_user.Name == null||string.IsNullOrEmpty(cs_user.Name.Trim()))
{
return Json(new { status = 401 });
}
if (cs_user.Age == 0||cs_user.Age==null)
{
return Json(new { status = 402 });
}
bd.Name = cs_user.Name;
bd.Age = cs_user.Age;
bd.Emal = cs_user.Emal;
bd.Phone = cs_user.Phone;
tv.CS_Users.Add(bd);
tv.SaveChanges();
return Json(new { status = 200 });
}
}
else
{
return Json(new { status = 500 });
}
}
catch (Exception ex)
{
return Json(new { status = 500 });
throw;
}
}
标签:status,bd,return,Ajax,user,cs,new From: https://www.cnblogs.com/lucy12/p/16716144.html