public ActionResult Create(string 帳號, string 密碼, string 角色, string[] 限) { string userid = 帳號; var tempMember = db.會員.Where(m => m.帳號 == userid).FirstOrDefault(); if (tempMember != null) { ViewBag.IsMember = true; return(View()); } string Permission = "R"; if (限 != null) { for (int i = 0; i < 限.Length; i++) { Permission += 限[i]; } } 會員 member = new 會員(); member.帳號 = 帳號; member.密碼 = 密碼; member.角色 = 角色; member.限 = Permission; db.會員.Add(member); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create(string fId, string fName, decimal fPrice, HttpPostedFileBase fImg) { //處理圖檔上傳 string fileName = ""; if (fImg != null) { if (fImg.ContentLength > 0) { fileName = System.IO.Path.GetFileName(fImg.FileName); //取得檔案的檔名 fImg.SaveAs(Server.MapPath("~/images/" + fileName)); //將檔案存到Images資料夾下 } } //處理圖檔上傳end tProduct product = new tProduct(); product.fId = fId; product.fName = fName; product.fPrice = fPrice; product.fImg = fileName; db.tProduct.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); //導向Index的Action方法 }
public ActionResult Create(string fId, string fName, decimal fPrice, HttpPostedFileBase fImg) { //處理圖檔上傳 string fileName = ""; if (fImg != null) { if (fImg.ContentLength > 0) { fileName = System.IO.Path.GetFileName(fImg.FileName); fImg.SaveAs(Server.MapPath("~/images/" + fileName));//MapPath:把邏輯路徑,轉成實體路徑。//將檔案存到Images資料夾下 } } //新增至db //步驟1,先將東西丟至module tProduct product = new tProduct(); product.fId = fId; product.fName = fName; product.fPrice = fPrice; product.fImg = fileName; db.tProduct.Add(product); //步驟2,存至db //多出來的做新增 //不一樣的做修改 db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Delete(string fId) { //依網址傳來的fId編號取得要刪除的產品記錄 var product = db.tProduct.Where(m => m.fId == fId).FirstOrDefault(); string fileName = product.fImg; //取得要刪除產品的圖檔 if (fileName != "") { //刪除指定圖檔 System.IO.File.Delete(Server.MapPath("~/Images") + "/" + fileName); } db.tProduct.Remove(product); db.SaveChanges(); //依編號刪除產品記錄 return(RedirectToAction("Index")); }
public ActionResult Create(string 類別名稱) { string editDate = DateTime.Now.ToString("yyyyMMddHHmmss"); var category = new 產品類別(); category.類別名稱 = 類別名稱; category.編輯者 = User.Identity.Name; category.建立日 = editDate; category.修改日 = editDate; db.產品類別.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); }
protected void Btn_Create_Click(object sender, EventArgs e) { try { // 圖檔儲存 string fileName = ""; if (FileUpload1.HasFile) { FileUpload1.SaveAs(Server.MapPath("Images") + "/" + FileUpload1.FileName); fileName = FileUpload1.FileName; } // 新增紀錄 dbProductEntities db = new dbProductEntities(); tProduct product = new tProduct(); product.fId = txtId.Text; product.fName = txtName.Text; product.fPrice = decimal.Parse(txtPrice.Text); product.fImg = fileName; db.tProduct.Add(product); db.SaveChanges(); // 轉向 Index.aspx Response.Redirect("Index.aspx"); } catch (Exception ex) { lblError.Text = ex.Message; } }
// 當控制項的 CommandName 按鈕被按時會觸發 RowCommand 事件 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { string fId = e.CommandArgument.ToString(); if (e.CommandName == "編輯") { Response.Redirect("Edit.aspx?fId=" + fId); } else if (e.CommandName == "刪除") { // 取得目前的產品 var product = db.tProduct.Where(m => m.fId == fId).FirstOrDefault(); // 取得產品圖檔 string fileName = product.fImg; if (fileName != "") { // 刪除指定圖檔 System.IO.File.Delete(Server.MapPath("~/Images") + "/" + fileName); } // 刪除指定產品 db.tProduct.Remove(product); db.SaveChanges(); LoadData(); } }
public ActionResult Create(string 產品編號, string 品名, int 單價, HttpPostedFileBase fImg, int 類別編號) { var tempProduct = db.產品資料.FirstOrDefault(x => x.產品編號 == 產品編號); if (tempProduct != null) { ViewBag.IsProduct = true; ViewBag.Category = db.產品類別.ToList(); return(View()); } string fileName = "question.png"; if (fImg != null) { if (fImg.ContentLength > 0) { fileName = Guid.NewGuid().ToString() + ".jpg"; var path = string.Format("{0}/{1}", Server.MapPath("~/Images"), fileName); fImg.SaveAs(path); } } string editDate = DateTime.Now.ToString("yyyyMMddHHmmss"); var product = new 產品資料(); product.產品編號 = 產品編號; product.品名 = 品名; product.單價 = 單價; product.圖示 = fileName; product.類別編號 = 類別編號; product.編輯者 = User.Identity.Name; product.建立日 = editDate; product.修改日 = editDate; db.產品資料.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create(string fid, string fname, decimal fprice, HttpPostedFileBase fImg) { string filename = ""; if (fImg != null) { if (fImg.ContentLength > 0) { filename = System.IO.Path.GetFileName(fImg.FileName);//取得檔案名稱 fImg.SaveAs(Server.MapPath("~/images/" + filename)); } } tProduct newproduct = new tProduct(); newproduct.fId = fid; newproduct.fName = fname; newproduct.fPrice = fprice; newproduct.fImg = filename; db.tProduct.Add(newproduct); db.SaveChanges(); var products = db.tProduct.ToList(); return(View("Index", products)); }
protected void Btn_Save_Click(object sender, EventArgs e) { string fId, fileName; fId = txtId.Text; var product = db.tProduct.Where(m => m.fId == fId).FirstOrDefault(); // 圖檔儲存 fileName = product.fImg; if (FileUpload1.HasFile) { FileUpload1.SaveAs(Server.MapPath("Images") + "/" + FileUpload1.FileName); fileName = FileUpload1.FileName; } // 修改紀錄 product.fName = txtName.Text; product.fPrice = decimal.Parse(txtPrice.Text); product.fImg = fileName; db.SaveChanges(); // 轉向 Index.aspx Response.Redirect("Index.aspx"); }