System.ArgumentNullException:“值不能为空。参数名称:entity”

时间:2020-06-07 19:50:55

标签: c#

我收到此错误System.ArgumentNullException:“ db.User.Remove(a)中的“值不能为空。参数名称:entity”,下面的代码:如何帮助修复它。谢谢

['d', 'a', 'b', 'c']
['c', 'd', 'a', 'b']
['b', 'c', 'd', 'a']
['a', 'b', 'c', 'd']
[['d', 'a', 'b'], ['c', 'd', 'a'], ['b', 'c', 'd'], ['a', 'b', 'c', 'd']]

对于此删除,我具有此视图,该视图类似于模型类别

 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 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

[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");
    }

the exception

索引2的视图

@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>

2 个答案:

答案 0 :(得分:0)

    [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()可以返回默认值-在类情况下,返回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 WebApp.Models.Category <!--This is wrong-->
@model WebApp.Models.User

@{
    ViewBag.Title = "Delete";
}
....

答案 1 :(得分:0)

api应该是[HttpDelete]而不是Post,并且仅传递ID而不是整个用户对象是黄油

[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");
}

还有您的问题,可能变量“ a”中的用户可能为空,请检查其是否为空

{{1}}