I’m working on a project (ASP.NET MVC5) for school but today I ran into this error even the same code yesterday is exactly like this 100% but still ran and today when i open it again it run to error. I’m already trying to fix this for a long hours but nothing works. I don’t know why it null anyway, i have tried to fix a lot but still get null when i put the edit ( SuaCT in the CT View ), and also the Details ( ChiTietCT ) and Delete ( XoaCT ) is get null errors too, its seem when i click on these buttons, it can not get the data from the list to edit, details and delete.
My view SuaCT.cshtml
@model STMA.Models.CONGTHUC
@{
ViewBag.Title = "SuaCT";
}
<h2>SuaCT</h2>
<link href="~/Content/SuaCT.css" rel="stylesheet" type="text/css" />
<center><h2 style="font-family: Georgia, serif; margin-top: 30px;">Sửa Công Thức</h2></center>
<div class="container-fluid">
<form class="col-lg-12" method="post" action="SuaCT">
<center>
<div class="col-md-5">
<div>
<select name="MaMA" aria-label="Chọn Món Ăn">
<option>@Model.MaMA</option> <!--Errors here-->
</select>
</div>
<div>
<select name="MaNL" aria-label="Chọn Nguyên Liệu">
<option>@Model.MaNL</option>
</select>
</div>
<div>
<input type="number" min="0" name="TrongLuong" value="" />
</div>
<div>
<select name="DVT" aria-label="Chọn DVT" class="select">
<optgroup label="Trọng Lượng">
<option value="gram">gram</option>
<option value="mg">mg</option>
<option value="kg">kg</option>
</optgroup>
<optgroup label="Thể Tích">
<option value="ml">ml</option>
<option value="lít">l</option>
</optgroup>
</select>
</div>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="✎" class="btn btn-default" />
<a href="@Url.Action("CT")" class="btn">➜</a>
</div>
</div>
</center>
</form>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
My CongThuc Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using STMA.Models;
namespace STMA.Controllers
{
public class CongThucController : Controller
{
// GET: CongThuc
// GET: Last
TIEULUANEntities data = new TIEULUANEntities();
public ActionResult CT()
{
return View(data.CONGTHUCs.ToList());
}
public ActionResult ThemCT()
{
ViewBag.dropNL = new SelectList(data.NGUYENLIEUx, "MaNL", "TenNL");
ViewBag.dropMA = new SelectList(data.MONANs, "MaMA", "TenMA");
return View();
}
[HttpPost]
public ActionResult ThemCT(CONGTHUC ct)
{
ViewBag.dropNL = new SelectList(data.NGUYENLIEUx, "MaNL", "TenNL");
ViewBag.dropMA = new SelectList(data.MONANs, "MaMA", "TenMA");
data.CONGTHUCs.Add(ct);
data.SaveChanges();
return RedirectToAction("CT");
}
public ActionResult SuaCT(string mact1, string mact2)
{
CONGTHUC ct = data.CONGTHUCs.Find(new object [] { mact1, mact2 });
return View(ct);
}
[HttpPost]
public ActionResult SuaCT(string mact1, string mact2, CONGTHUC ct)
{
CONGTHUC ctt = data.CONGTHUCs.Find( new object[] { mact1 , mact2 });
if (ctt == null)
{
return HttpNotFound();
}
// Gán giá trị cho các thuộc tính của ctt từ ct
ctt.TrongLuong = ct.TrongLuong;
ctt.DVT = ct.DVT;
// Lưu thay đổi vào cơ sở dữ liệu
data.SaveChanges();
return RedirectToAction("CT");
}
public ActionResult ChiTietCT(string mact1, string mact2)
{
ViewBag.dropNL = new SelectList(data.NGUYENLIEUx, "MaNL", "TenNL");
ViewBag.dropMA = new SelectList(data.MONANs, "MaMA", "TenMA");
CONGTHUC ct = data.CONGTHUCs.Find(new object[] { mact1, mact2 });
return View(ct);
}
public ActionResult XoaCT(string mact1, string mact2)
{
CONGTHUC ct = data.CONGTHUCs.Find( new object[] { mact1, mact2});
data.CONGTHUCs.Remove(ct);
data.SaveChanges();
return RedirectToAction("CT");
}
}
}
My List View
@model IEnumerable<STMA.Models.CONGTHUC>
@{
ViewBag.Title = "CT";
}
<center><h2 class="my-5" style="font-family: Georgia, serif;">Danh Sách Công Thức</h2></center>
<p>
<a href="@Url.Action("ThemCT","CongThuc")" name="ThemCT" class="btn btn-outline-warning text-dark mx-2"><b>Thêm</b></a>
</p>
<div class="container-fluid" style="font-size: 17px;">
<div class="row">
<div class="col-lg-12">
<table class="table table-borderless" style="overflow: auto">
<thead>
<tr class="col-lg-12 table-dark">
<th>
<label>Tên Món</label>
</th>
<th>
<label>Tên Nguyên Liệu</label>
</th>
<th>
<label>Trọng Lượng</label>
</th>
<th>
<label>Đơn Vị Tính</label>
</th>
<th class="col-1"></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="col-lg-12">
<td>
@Html.DisplayFor(Model => item.MONAN.TenMA)
</td>
<td>
@Html.DisplayFor(Model => item.NGUYENLIEU.TenNL)
</td>
<td>
@item.TrongLuong
</td>
<td>
@item.DVT
</td>
<td class="btn-group" role="group">
<a class="btn btn-success" href="@Url.Action("SuaCT", "CongThuc", new { mact1 = item.MaMA , mact2 = item.MaNL })">✎</a>
<a class="btn btn-primary" href="@Url.Action("ChiTietCT", "CongThuc", new {mact1 = item.MaMA , mact2 = item.MaNL })">☰</a>
<a class="btn btn-danger" href="@Url.Action("XoaCT", "CongThuc", new { mact1 = item.MaMA , mact2 = item.MaNL })">⨉</a>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
Vu Ba Luc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.