public static ColumnInfo Create(ColumnInfo model) { if (model.Id == 0) { //Insert int id = CategoryManage.AddColumnInfo(model); } else { //Update CategoryManage.UpdateColumnInfo(model); } return model; }
/// <summary> /// 判断某子项的所有父项中是否存在指定父ID /// </summary> /// <param name="list">集合</param> /// <param name="child">子项</param> /// <param name="compareParentId">父ID</param> /// <returns></returns> private static bool CompareParentID(List<ColumnInfo> list, ColumnInfo child, int compareParentId) { if (child.ParentId == compareParentId) return true; var category = list.Find(c => c.Id == child.ParentId); while (category != null) { if (category.ParentId == compareParentId) return true; var nextParentId = category.ParentId; category = list.Find(c => c.Id == nextParentId); } return false; }
/// <summary> /// 更新信息 /// </summary> /// <param name="model"></param> public static int UpdateColumnInfo(ColumnInfo model) { string strSQL = "UPDATE Categories SET Name = @Name,Alias = @Alias,Sort = @Sort,IsDeleted = @IsDeleted WHERE Id = @Id AND [Type] = 'column'"; SqlParameter[] parms = { new SqlParameter("Name",SqlDbType.NVarChar), new SqlParameter("Alias",SqlDbType.NVarChar), new SqlParameter("Sort",SqlDbType.Int), new SqlParameter("IsDeleted",SqlDbType.Bit), new SqlParameter("Id",SqlDbType.Int), }; parms[0].Value = model.Name; parms[1].Value = string.IsNullOrEmpty(model.Alias) ? string.Empty : model.Alias; parms[2].Value = model.Sort; parms[3].Value = model.IsDeleted; parms[4].Value = model.Id; return SQLPlus.ExecuteNonQuery(CommandType.Text,strSQL,parms); }
/// <summary> /// 添加栏目分类 /// </summary> /// <param name="model"></param> /// <returns></returns> public static int AddColumnInfo(ColumnInfo model) { string strSQL = "INSERT INTO Categories(ParentId,RootId,ParentIds,Name,Alias,Sort,IsDeleted,[Type],CreateDateTime) VALUES(@ParentId,@RootId,@ParentIds,@Name,@Alias,@Sort,@IsDeleted,'column',GETDATE());SELECT @@IDENTITY;"; SqlParameter[] param = { new SqlParameter("Name",SqlDbType.NVarChar), new SqlParameter("Alias",SqlDbType.VarChar), new SqlParameter("Sort",SqlDbType.Int), new SqlParameter("IsDeleted",SqlDbType.Bit), new SqlParameter("ParentId",SqlDbType.Int), new SqlParameter("RootId",SqlDbType.Int), new SqlParameter("ParentIds",SqlDbType.VarChar), }; param[0].Value = model.Name; param[1].Value = model.Alias.ToLower(); param[2].Value = model.Sort; param[3].Value = model.IsDeleted; param[4].Value = model.ParentId; param[5].Value = model.RootId; param[6].Value = model.ParentIds; return Convert.ToInt32(SQLPlus.ExecuteScalar(CommandType.Text, strSQL, param)); }
public ActionResult Edit(ColumnInfo oldModel) { if(oldModel.Id == 0){ return RedirectToAction("List"); } if (ModelState.IsValid) { ColumnService.Create(oldModel); ViewBag.Msg = "更新成功,<a href=\"list\">返回</a>"; } return View(); }