/// <summary> /// 更新 /// </summary> /// <param name="model"></param> /// <returns></returns> public static BlogPostInfo Update(BlogPostInfo model) { if (model.Id == 0) { int id = BlogPostManage.Insert(model); model.Id = id; } else { BlogPostManage.Update(model); } return model; }
public ActionResult Edit(BlogPostInfo oldModel, FormCollection fc) { bool error = false; bool isAdd = oldModel.Id == 0; int userId = PlantEngContext.Current.UserId; string userName = PlantEngContext.Current.UserName; if (string.IsNullOrEmpty(oldModel.Title)) { error = true; ModelState.AddModelError("TITLEEMPTY", "标题不能为空"); } if (string.IsNullOrEmpty(oldModel.Content)) { error = true; ModelState.AddModelError("CONTENTEMPTY", "内容不能为空"); } //系统分类 oldModel.SystemCategoryId = Utils.StrToInt(fc["ddlSystemCategory"], 0); if(oldModel.SystemCategoryId == 0){ error = true; ModelState.AddModelError("SYSTEMCATERROR","请选择系统分类"); } if (!error && ModelState.IsValid) { oldModel.UserId = userId; oldModel.UserName = userName; //获得系统分类名称 oldModel.SystemCategoryName = PlantEng.Services.Category.TechService.GetById(oldModel.SystemCategoryId).Name; BlogPostService.Update(oldModel); if (isAdd) { ViewBag.Msg = "添加成功,继续[<a href=\"/accounts/edit\">添加</a>]或[<a href=\"/accounts/blog/\">返回</a>]"; } else { ViewBag.Msg = "更新成功,<a href=\"/accounts/blog/\">返回</a>"; } } return View(oldModel); }
/// <summary> /// 更新 /// </summary> /// <param name="model"></param> /// <returns></returns> public static int Update(BlogPostInfo model) { string strSQL = "UPDATE BlogPosts SET Title = @Title,Content = @Content,SystemCategoryId = @SystemCategoryId,Tags = @Tags,SystemCategoryName = @SystemCategoryName WHERE Id = @Id AND UserId = @UserId"; SqlParameter[] parms = { new SqlParameter("Id",SqlDbType.Int), new SqlParameter("UserId",SqlDbType.Int), new SqlParameter("Title",SqlDbType.NVarChar), new SqlParameter("Content",SqlDbType.NVarChar), new SqlParameter("SystemCategoryId",SqlDbType.Int), new SqlParameter("Tags",SqlDbType.NVarChar), new SqlParameter("SystemCategoryName",SqlDbType.NVarChar), }; parms[0].Value = model.Id; parms[1].Value = model.UserId; parms[2].Value = string.IsNullOrEmpty(model.Title) ? string.Empty : model.Title; parms[3].Value = string.IsNullOrEmpty(model.Content) ? string.Empty : model.Content; parms[4].Value = model.SystemCategoryId; parms[5].Value = string.IsNullOrEmpty(model.Tags) ? string.Empty : model.Tags; parms[6].Value = model.SystemCategoryName; return SQLPlus.ExecuteNonQuery(CommandType.Text, strSQL, parms); }
/// <summary> /// 添加 /// </summary> /// <param name="model"></param> /// <returns></returns> public static int Insert(BlogPostInfo model) { string strSQL = "INSERT INTO BlogPosts(UserId,Title,Content,ViewCount,CreateDateTime,SystemCategoryId,Tags,UserName,SystemCategoryName) VALUES(@UserId,@Title,@Content,0,GETDATE(),@SystemCategoryId,@Tags,@UserName,@SystemCategoryName);SELECT @@IDENTITY;"; SqlParameter[] parms = { new SqlParameter("Id",SqlDbType.Int), new SqlParameter("UserId",SqlDbType.Int), new SqlParameter("Title",SqlDbType.NVarChar), new SqlParameter("Content",SqlDbType.NVarChar), new SqlParameter("SystemCategoryId",SqlDbType.Int), new SqlParameter("Tags",SqlDbType.NVarChar), new SqlParameter("UserName",SqlDbType.NVarChar), new SqlParameter("SystemCategoryName",SqlDbType.NVarChar), }; parms[0].Value = model.Id; parms[1].Value = model.UserId; parms[2].Value = string.IsNullOrEmpty(model.Title) ? string.Empty : model.Title; parms[3].Value = string.IsNullOrEmpty(model.Content) ? string.Empty : model.Content; parms[4].Value = model.SystemCategoryId; parms[5].Value = string.IsNullOrEmpty(model.Tags) ? string.Empty : model.Tags; parms[6].Value = model.UserName; parms[7].Value = model.SystemCategoryName; return Convert.ToInt32(SQLPlus.ExecuteScalar(CommandType.Text,strSQL,parms)); }