I work on an E-commerce website using .NET as my language and LINQ as database but when I run the code my update function did not work and get exception throw System.NullReferenceException
I find my id from database
[HttpGet] public ActionResult Suasach(int id) { SACH sach = data.SACHes.SingleOrDefault(n => n.Masach == id); if (sach == null) { Response.StatusCode = 404; return null; } ViewBag.MaCD = new SelectList(data.CHUDEs.ToList().OrderBy(n => n.TenChuDe), "MaCD", "TenChude"); ViewBag.MaNXB = new SelectList(data.NHAXUATBANs.ToList().OrderBy(n => n.TenNXB), "MaNXB", "TenNXB"); return View(sach); }
My view is this:
@model QlBanSach.Models.SACH
@{
ViewBag.Title = "Suasach";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<h2>ĐIỀU CHỈNH THÔNG TIN SÁCH</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Masach)
<div class="form-group">
@Html.LabelFor(model => model.Tensach, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tensach, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Tensach, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Giaban, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Giaban, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Giaban, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Mota, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<textarea name="Mota">@Model.Mota</textarea>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Anhbia, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">Chọn ảnh mới
<input type="file" name="fileUpload"/>
<img src="@Url.Content("~/images/"+Model.Anhbia)" width="120px"/>(Ảnh hiện tại)
@ViewBag.Thongbao
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Ngaycapnhat, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="date" name="Ngaycapnhat" />
@Html.ValidationMessageFor(model => model.Ngaycapnhat, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Soluongton, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Soluongton, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Soluongton, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MaCD, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("MaCD")
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MaNXB, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("MaNXB")
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Sửa" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("VỀ TRANG QUẢN LÝ SÁCH", "Sach")
</div>
My update function is this:
[HttpPost]
[ValidateInput(false)]
public ActionResult Suasach(SACH sach, HttpPostedFileBase fileupload)
{
ViewBag.MaCD = new SelectList(data.CHUDEs.ToList().OrderBy(n => n.TenChuDe), "MaCD", "TenChude");
ViewBag.MaNXB = new SelectList(data.NHAXUATBANs.ToList().OrderBy(n => n.TenNXB), "MaNXB", "TenNXB");
// TODO: Add update logic here
if (fileupload == null)
{
ViewBag.Thongbao = "Vui lòng chọn ảnh bìa";
return View();
}
else
{
if (ModelState.IsValid)
{
var fileName = Path.GetFileName(fileupload.FileName);
var path = Path.Combine(Server.MapPath("~/images"), fileName);
if (System.IO.File.Exists(path))
{
ViewBag.Thongbao = "Hình ảnh đã tồn tại";
}
else
{
fileupload.SaveAs(path);
}
sach.Anhbia = fileName;
UpdateModel(sach);
data.SubmitChanges();
}
}
return RedirectToAction("Sach");
}
But when I run it my update function did not work, it got exception throw System.NullReferenceException 'Object reference not set to an instance of an object.'
System.Web.Mvc.WebViewPage<TModel>.Model.get returned null.
Is there a solution to this?