I get this error System.ArgumentNullException: "Value cannot be null. Parameter name: entity" at db.User.Remove(a),following code: how can help to fix it.Thanks
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);
}
For this delete i have this view which get like model a category
@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>
And for this method
[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");
}
have this view which get user like @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>
The view for the 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");
}
Because -.FirstOrDefault() can return the default value if condition not found - in class case, returns null. You cannot a user which does not exist. (Remove(null))
Same thing you need to add in your other method
EDIT: After viewing the code - the problem is on the delete action you are returning a category instead of a user:
[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);
}
... For this delete i have this view which get like model a category
@model WebApp.Models.Category <!--This is wrong-->
@model WebApp.Models.User
@{
ViewBag.Title = "Delete";
}
....
the api should be [HttpDelete] not Post and it is butter to pass just Id, not whole user 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");
}
and your problem, maybe there is chance for user in variable "a" been null, check if it is null or not
[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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.