//#region 保存按钮,保存为草稿
$(".saveButton").click(function () {
var MaintenanceOrPartsNo = $('#MaintenanceOrPartsNo').val();
$.messager.confirm('确认', '您确认保存吗?', function (r) {
if (r) {
$.post("/Maintenances/BaoCun", { mainInfo: decodeURIComponent(JSON.stringify($("#myform").serializeArray())), }, function (data){
if (data.issuccess) {
//$.messager.confirm("操作提示", "保存成功,是否跳到保养一览!", function (r) {
// if (r) {
toTab("", "/Maintenances/MaintenanceLists", "保养一览");
var jq = top.jQuery;
var tt = jq('#tt');
tt.tabs("close", "保养详情1" + MaintenanceOrPartsNo);
// }
//});
} else {
$.messager.alert("操作提示", "保存失败!");
}
});
}
});
});
//#endregion
后台controller
#region 保存功能
public JsonResult BaoCun(string mainInfo)
{
int result = 0;
string msg = "操作失败";
try
{
Maintenance model =new Maintenance();
List<formModel> objModel = newSystem.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<formModel>>(mainInfo);
foreach (formModel info in objModel)
{
if (!string.IsNullOrEmpty(info.value))
{
if (info.name.Equals("MaintenanceOrPartsNo"))
model.MaintenanceOrPartsNo = info.value;
}
}
foreach (formModel info in objModel)
{
PropertyInfo[]propertys = model.GetType().GetProperties();
foreach (PropertyInfo p in propertys)
{
if (!string.IsNullOrEmpty(info.value))
{
if (p.Name.Equals(info.name))
if (p.PropertyType == typeof(string))
{
p.SetValue(model, info.value, null);
}
else if (p.PropertyType == typeof(int) || p.PropertyType == typeof(Nullable<int>))
{
p.SetValue(model, int.Parse(info.value.ToString()),null);
}
else if (p.PropertyType == typeof(DateTime) || p.PropertyType== typeof(Nullable<DateTime>))
{
p.SetValue(model, Convert.ToDateTime(info.value.ToString()),null);
}
else if (p.PropertyType == typeof(float) || p.PropertyType == typeof(Nullable<float>))
{
p.SetValue(model, float.Parse(info.value.ToString()), null);
}
else if (p.PropertyType == typeof(double) || p.PropertyType == typeof(Nullable<double>))
{
p.SetValue(model, double.Parse(info.value.ToString()),null);
}
}
else
continue;
}
}
Dictionary<string, dynamic> dic = new Dictionary<string, dynamic>();
dic.Add("Maintenance", model);
result = MaintenanceBLL.InsertList(dic);
if (result > 0)
{
msg = "操作成功";
}
else
msg = "操作失败";
}
catch (Exception ex)
{
result = 0;
msg = "操作失败";
}
object obj = new
{
issuccess = (result > 0 ? true : false),
msg = msg
};
return Json(obj);
}
#endregion