![](/img/trans.png)
[英]System.ArgumentNullException: 'Value cannot be null. Parameter name: key'
[英]System.ArgumentNullException: “Value cannot be null. Parameter name: entity”
我在 db.User.Remove(a) 處收到此錯誤 System.ArgumentNullException:“值不能為 null。參數名稱:實體”,以下代碼:如何幫助修復它。謝謝
NewEntities2 db = new NewEntities2();
[HttpGet]
public ActionResult Index2()
{
var a = db.User.ToList();
return View(a);
}
[HttpGet]
public ActionResult DeleteUser(int id)
{
var a = db.User.Where(x => x.User_Id == id).FirstOrDefault();`enter code here`
return View(a);
}
[HttpPost]
public ActionResult DeleteUser(User user)
{
var a = db.User.Where(x => x.User_Id == user.User_Id).FirstOrDefault();
db.User.Remove(a);
db.SaveChanges();
return RedirectToAction("Index2");
}
[HttpGet]
public ActionResult Delete(int? id)
{
var cat1 = db.Category.Where(x => x.Id == id).FirstOrDefault();
return View(cat1);
}
[HttpPost]
public ActionResult Delete(Category category)
{
var cat1 = db.Category.Where(x => x.Id == category.Id).FirstOrDefault();
db.Category.Remove(cat1);
db.SaveChanges();
return RedirectToAction("Index");
}
public ActionResult Index()
{
var c = db.Category.ToList();
return View(c);
}
對於這個刪除,我有這個視圖,它就像 model 一個類別
@model WebApp.Models.Category
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Category</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
</dl>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>
而對於這種方法
[HttpGet]
public ActionResult Index2()
{
var a = db.User.ToList();
return View(a);
}
[HttpGet]
public ActionResult DeleteUser(int ids)
{
var a = db.User.FirstOrDefault(x=>x.User_Id==ids);
return View(a);
}
[HttpPost]
public ActionResult DeleteUser(User user)
{
var a = db.User.Where(x => x.User_Id == user.User_Id).FirstOrDefault();
db.User.Remove(a);
db.SaveChanges();
return RedirectToAction("Index2");
}
有這個視圖可以讓用戶喜歡@model
@model WebApp.Models.User
@{
ViewBag.Title = "DeleteUser";
}
<h2>DeleteUser</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>User</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.User_Id)
</dt>
<dd>
@Html.DisplayFor(model => model.User_Id)
</dd>
<dt>
@Html.DisplayNameFor(model => model.User_login)
</dt>
<dd>
@Html.DisplayFor(model => model.User_login)
</dd>
<dt>
@Html.DisplayNameFor(model => model.User_password)
</dt>
<dd>
@Html.DisplayFor(model => model.User_password)
</dd>
</dl>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>
Index2 的視圖
@model IEnumerable<WebApp.Models.User>
@{
ViewBag.Title = "Index2";
}
<h2>Index2</h2>
<p>
@Html.ActionLink("Create User", "Login")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.User_Id)
</th>
<th>
@Html.DisplayNameFor(model => model.User_login)
</th>
<th>
@Html.DisplayNameFor(model => model.User_password)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.User_Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.User_login)
</td>
<td>
@Html.DisplayFor(modelItem => item.User_password)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete User", "DeleteUser", new {ids=item.User_Id })
</td>
</tr>
}
</table>
[HttpPost]
public ActionResult DeleteUser(User user)
{
var a = db.User.Where(x => x.User_Id == user.User_Id).FirstOrDefault();
if(a!=null)
{
db.User.Remove(a);
db.SaveChanges();
}
else
{
//Handle 'user with id=xxx not found'
}
return RedirectToAction("Index2");
}
因為 -.FirstOrDefault() 可以在未找到條件時返回默認值 - 在 class 情況下,返回 null。 您不能使用不存在的用戶。 (刪除(空))
您需要在其他方法中添加相同的內容
編輯:查看代碼后-問題出在刪除操作上,您返回的是類別而不是用戶:
[HttpGet]
public ActionResult Delete(int? id)
{
//return a user, not category.
//change this--> var cat1 = db.Category.Where(x => x.Id == id).FirstOrDefault(); to:
var user = db.User.FirstOrDefault(x => x.User_Id == id);
return View(user);
}
...對於這個刪除,我有這樣的觀點,就像 model 一個類別
@model WebApp.Models.Category <!--This is wrong-->
@model WebApp.Models.User
@{
ViewBag.Title = "Delete";
}
....
api 應該是 [HttpDelete] 而不是 Post 並且只傳遞 Id 是黃油,而不是整個用戶 object
[HttpPost]
public ActionResult DeleteUser(User user)
{
var a = db.User.Where(x => x.User_Id == user.User_Id).FirstOrDefault();
db.User.Remove(a);
db.SaveChanges();
return RedirectToAction("Index2");
}
你的問題,也許變量“a”中的用戶有機會是 null,檢查它是否是 null
[HttpDelete]
public ActionResult DeleteUser(long userId)
{
var user = db.User.FirstOrDefault(x => x.User_Id == userId);
if(user is null)
{ throw new Exception("there is no user with id "+ userId); }
db.User.Remove(user );
db.SaveChanges();
return RedirectToAction("Index2");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.