Chào mọi người,
Em đang code website bán hàng đến phần thêm, xóa, sửa sản phẩm trang admin.
Em code xong nhưng gặp vấn đề là khi điền xong form thêm sản phẩm thì nó không cập nhật thêm sản phẩm và không return về trang index danh sách sản phẩm. Bấm Lưu nó kiểu nó kiểu sẽ load lại trang.
Sau đây là code của em, mong mọi người giúp hix thời gian cấp bách quá e sắp nộp rồi :<
File Create.cshtml
@{
ViewBag.Title = "Create";
Layout = "~/Areas/Admin/Views/Shared/_MainLayoutAdmin.cshtml";
}
<style>
input[type=submit] {
width: 10%;
background-color: #152036;
color: white;
padding: 14px 20px;
font: inherit;
border: none;
border-radius: 4px;
cursor: pointer;
}
</style>
<div class="single-pro-review-area">
@using (Html.BeginForm("Create", "SanPhamAdmin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="review-tab-pro-inner">
<ul id="myTab3" class="tab-review-design">
<li class="active"><a href="#description"><i class="icon nalika-edit" aria-hidden="true"></i> Thêm sản phẩm</a></li>
</ul>
<div id="myTabContent" class="tab-content custom-product-edit">
<div class="product-tab-list tab-pane fade active in" id="description">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="review-content-section">
<div class="input-group mg-b-pro-edt">
<span class="input-group-addon"><i class="icon nalika-user" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="MASP" placeholder="Mã Sản Phẩm" required>
</div>
<div class="input-group mg-b-pro-edt">
<span class="input-group-addon"><i class="icon nalika-edit" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="TENSP" placeholder="Tên Sản Phẩm" required>
</div>
<div class="input-group mg-b-pro-edt">
<span class="input-group-addon"><i class="icon nalika-edit" aria-hidden="true"></i></span>
<input type="number" class="form-control" name="GIA" placeholder="Giá" required>
</div>
<div class="input-group mg-b-pro-edt">
<span class="input-group-addon"><i class="icon nalika-edit" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="TINHTRANG" placeholder="Tình Trạng" required>
</div>
<div class="input-group mg-b-pro-edt">
<span class="input-group-addon"><i class="icon nalika-edit" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="MOTA" placeholder="Mô Tả" required>
</div>
<div class="input-group mg-b-pro-edt">
<span class="input-group-addon"><i class="icon nalika-edit" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="HINHCHINH" placeholder="Hình Ảnh">
<span class="input-group-addon"><i aria-hidden="true"></i></span>
<input type="file" class="form-control" id="file-upload" onchange="previewFile();" name="HINHCHINH" placeholder="Mã Hình Ảnh" required>
</div>
<div class="input-group mg-b-pro-edt">
<span class="input-group-addon"><i class="icon nalika-edit" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="MALOAI" placeholder="Mã Loại" required>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="text-center custom-pro-edt-ds">
<input type="submit" value="Lưu">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
}
</div>
File SanPhamAdminController
// GET: Admin/SanPham
public ActionResult Index()
{
return View(ShopOnlineBUS.DanhSachSP());
}
// GET: Admin/SanPham/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Admin/SanPham/Create
public ActionResult Create()
{
return View();
}
// POST: Admin/SanPhamAdmin/Create
[HttpPost]
[ValidateInput(false)]
public ActionResult Create(SANPHAM sp)
{
try
{
// TODO: Add insert logic here
ShopOnlineBUS.InsertSP(sp);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
File ShopOnlineBUS.cs
public static void InsertSP(SANPHAM sp)
{
var db = new ShopOnlineConnectionDB();
db.Insert(sp);
}
Giao diện
Debug