C# System.ArgumentNullException:“;值不能为null。参数名称:实体";
我得到这个错误System.ArgumentNullException:“值不能为null。参数名:entity”位于db.User.Remove(a),以下代码:如何帮助修复它。谢谢C# System.ArgumentNullException:“;值不能为null。参数名称:实体";,c#,C#,我得到这个错误System.ArgumentNullException:“值不能为null。参数名:entity”位于db.User.Remove(a),以下代码:如何帮助修复它。谢谢 NewEntities2 db = new NewEntities2(); [HttpGet] public ActionResult Index2() { var a = db.User.ToList(); return View(a); }
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);
}
对于这个删除,我有一个类似于模型a类别的视图
@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>
@model WebApp.Models.Category <!--This is wrong-->
@model WebApp.Models.User
@{
ViewBag.Title = "Delete";
}
....
有这样一个视图,它得到了用户喜欢的@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>
@model WebApp.Models.User
@{
ViewBag.Title=“DeleteUser”;
}
删除用户
是否确实要删除此项?
使用者
@DisplayNameFor(model=>model.User\u Id)
@DisplayFor(model=>model.User\u Id)
@DisplayNameFor(model=>model.User\u登录)
@DisplayFor(model=>model.User\u登录)
@DisplayNameFor(model=>model.User\u密码)
@DisplayFor(model=>model.User\u密码)
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
|
@ActionLink(“返回列表”、“索引”)
}
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>
@model IEnumerable
@{
ViewBag.Title=“Index2”;
}
Index2
@ActionLink(“创建用户”、“登录”)
@DisplayNameFor(model=>model.User\u Id)
@DisplayNameFor(model=>model.User\u登录)
@DisplayNameFor(model=>model.User\u密码)
@foreach(模型中的var项目){
@DisplayFor(modeleItem=>item.User\u Id)
@DisplayFor(modelItem=>item.User\u登录)
@DisplayFor(modelItem=>item.User\u密码)
@ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“删除用户”,“删除用户”,新的{ids=item.User\u Id})
}
因为-.FirstOrDefault()可以在未找到条件时返回默认值-在类case中,返回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);
}
。。。
对于这个删除,我有一个类似于模型a类别的视图
@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>
@model WebApp.Models.Category <!--This is wrong-->
@model WebApp.Models.User
@{
ViewBag.Title = "Delete";
}
....
@model WebApp.Models.Category
@模型WebApp.Models.User
@{
ViewBag.Title=“删除”;
}
....
api应该是[HttpDelete]而不是Post,只传递Id,而不是整个用户对象是很重要的
[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");
}
谢谢你的回答,伙计,问题是我有这个用户,当我按下delete botton时,出现异常,我尝试使用我项目中的另一个表,它工作正常,但我不知道哪里有问题。如果您可以感谢,请提供帮助如果不查看整个代码(视图),很难说出来,如果您查看“DeleteUser”请求,是否在user.user\u Id中看到您的期望值?如果您愿意,请查看mate。这里有一个链接:。谢谢所有相关代码都应该在站点上,而不是在外部站点上,但我知道问题出在哪里,请参阅editActionResult Delete是针对类别的,您是对的,但ActionResult DeleteUser是针对Delete user的,在这段代码中会出现异常