mình làm bài tập này, lỗi là không thể thêm sản phẩm vào cơ sở dữ liệu, còn mà thêm trực tiếp tại sql thì lại được
1.thêm mới
2. khi nhấn vào create thì không xuất hiện trong bản
3.thêm trực tiếp từ sql
4… nó đã được thêm vào
5…code Control của hàm create
public ActionResult Createhanghoa()
{
return View();
}
[HttpPost, ActionName("Thêm hàng")]
public ActionResult Createhanghoa([Bind(Include = "Mahanghoa, Tenhanghoa")]Hanghoa hanghoa)
{
try
{
if (ModelState.IsValid)
{
// thêm mới sách
db.Hanghoas.Add(hanghoa);
db.SaveChanges();
}
}
catch (RetryLimitExceededException /*dex*/)
{
ModelState.AddModelError("", "Error seva Data");
}
//trả về trang xem sách với dnh sách mới
var listhanghoa = from s in db.Hanghoas
select s;
return View(listhanghoa);
}
6… code view của create
@model QLHanghoa.Hanghoa
@{
ViewBag.Title = "Createhanghoa";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Createhanghoa</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Hanghoa</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Tenhanghoa, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tenhanghoa)
@Html.ValidationMessageFor(model => model.Tenhanghoa)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "getlistanghoa")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}