[HttpGet] public ActionResult Edit(int? id) { if (id == null) return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest); Product product = objDBContext.Products.Find(id); if (product == null) return HttpNotFound(); else return View(product); }
[HttpPost] public ActionResult Edit(HttpPostedFileBase file) { try { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~\\App_Data\\Images"), fileName); file.SaveAs(path); } ViewBag.Message = "Upload successful"; return RedirectToAction("Index"); } catch { ViewBag.Message = "Upload failed"; return RedirectToAction("Uploads"); } }
<div class="form-group"> @Html.Label("Upload Image", new { @class = "control-label col-md-2" }) <fieldset> <div class="editor-field"> @Html.TextBox("file", "", new { type = "file" }) </div> <div class="editor-field"> <input type="submit" value="Upload" /> </div> </fieldset> </div>
<div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> </div>