Lỗi không thêm được sản phẩm trong c# vào cơ sở dữ liệu

mình đang làm dự án thử, khi mình click thêm sản phẩm mới thì không thấy dữ liệu mới thêm vào bản hiển thị, còn mình vào sql thêm trực tiếp sau đó chạy lại trình dyệt thì nó hiện lên sản phẩm vừa thêm,
1… thêm,

2. ko hiện trong bản


3. thêm trực tiếp vào sql

4.khi đã thêm trực tiếp

5…code view 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")
}
6.code Controll của 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);
        }
	}

Trùng topic

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?