表单提交时,更新的操作
//原程序的写法,意思就是,先查出数据库里面的记录,然后用提交过来的数据进行覆盖数据库里面的值,调用的方法是SetAttribute
public IActionResult UpdateSaleAfterDTID(int id, string proname, string isupdate, IFormCollection values, int orderid) { if (!string.IsNullOrEmpty(isupdate)) { //表单提交时触发事件 var fchelpAdd = new FormConfigHelp(db, Url); var AllConfigs = fchelpAdd.GetBodies(RouteData, "MainForm1"); var cvalue = values.Where(p => AllConfigs.Where(q => q.ClassName == "Model.SFSaleAfterDT").Select(q => q.ControlID).Contains(p.Key)).ToList(); var SFSaleAfterDTID = Convert.ToInt32(values["SaleAfterDTID"]); var sfdt = db.SFSaleAfterDT.Where(p => p.SaleAfterDTID == SFSaleAfterDTID).FirstOrDefault(); fchelpAdd.SetAttribute(sfdt, cvalue); db.SaveChanges(); PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference()); return UIHelper.Result(); } }
public void SetAttribute(object model, List<KeyValuePair<string, Microsoft.Extensions.Primitives.StringValues>> values) { var entityEntry = db.Entry(model); foreach (var item in values) { var DateValue = entityEntry.Property(item.Key); if (item.Value.ToString() == "") { DateValue.CurrentValue = null; } else if (DateValue.Metadata.ClrType.ToString().Contains("String")) { DateValue.CurrentValue = item.Value.ToString(); } else if (DateValue.Metadata.ClrType.ToString().Contains("Decimal")) { DateValue.CurrentValue = Convert.ToDecimal(item.Value); } else if (DateValue.Metadata.ClrType.ToString().Contains("Int32")) { int value; if (item.Value.ToString().ToLower() == "false") { value = 0; } else if (item.Value.ToString().ToLower() == "true") { value = 1; } else { value = Convert.ToInt32(item.Value); } DateValue.CurrentValue = value; } else if (DateValue.Metadata.ClrType.ToString().Contains("Int64")) { long value; if (item.Value.ToString().ToLower() == "false") { value = 0; } else if (item.Value.ToString().ToLower() == "true") { value = 1; } else { value = Convert.ToInt64(item.Value); } DateValue.CurrentValue = value; } else if (DateValue.Metadata.ClrType.ToString().Contains("DateTime")) { DateValue.CurrentValue = Convert.ToDateTime(item.Value); } else if (DateValue.Metadata.ClrType.ToString().Contains("Boolean")) { DateValue.CurrentValue = Convert.ToBoolean(item.Value); } else if (DateValue.Metadata.ClrType.ToString().Contains("Double")) { DateValue.CurrentValue = Convert.ToDouble(item.Value); } else { string a = DateValue.Metadata.ClrType.ToString(); } } }
下面是我的写法,直接把提交过来的数据创建成一个类,然后查数据库里面的数据,进行对比,如果数据库里的字段有值,创建的类的字段没有值,那么就把数据库里的值覆盖到创建的类的字段里,然后用创建的类去更新数据库,用的方法是JyfUpdateModel.updateModel
public IActionResult btnUpdateSFAccessoryGrid_Click(IFormCollection values, SFAccessory sfacc, string orderid, string isupdate, string rowIndex, string U9ID,int fnrid) { var dbAcModel = db.SFAccessory.Find(sfacc.AccessoryID); db.Entry(dbAcModel).State = EntityState.Detached;//不跟踪数据库里查出来的类 JyfUpdateModel.updateModel(dbAcModel,sfacc); db.Update(sfacc); db.SaveChanges(); } /// <summary> ///创建人:咖啡无眠 ///日 期:2019/12/23 ///描 述:更新实体,与数据库实体进行对比 /// </summary> public class JyfUpdateModel { public static void updateModel(object dataBaseObj, object newObj) { Type t = dataBaseObj.GetType(); //3.2.3?获取实体类所有的公共属性?? List<PropertyInfo> propertyInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); //3.2.4?创建实体属性字典集合?? Dictionary<string, PropertyInfo> dicPropertys = new Dictionary<string, PropertyInfo>(); //3.2.5?将实体属性中要修改的属性名?添加到字典集合中??键:属性名??值:属性对象?? propertyInfos.ForEach(p => { dicPropertys.Add(p.Name, p); }); Type t2 = newObj.GetType(); foreach (var item in t2.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList()) { if (dicPropertys.Keys.Contains(item.Name)) { try { if (item.GetValue(newObj) == null && dicPropertys[item.Name].GetValue(dataBaseObj) != null) { item.SetValue(newObj, dicPropertys[item.Name].GetValue(dataBaseObj)); } } catch (Exception ex) { throw; } } } } }