【问题标题】:System.ArgumentNullException: 'Value cannot be null. Parameter name: entity' i get this error while using POST methodeSystem.ArgumentNullException: '值不能为空。参数名称:实体'我在使用 POST 方法时收到此错误
【发布时间】:2021-05-04 19:41:58
【问题描述】:

嗨,当我使用 POSTMAN 时,任何人都可以帮助解决这个问题,我能够将数据发布到数据库。但是当我调用 POST Methode 时,我得到了这个错误。我猜我传递给 API Controller 的参数是 null 的问题。我该如何解决这个问题

客户模型

    public class Customers
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
}

MVC 控制器

        public ActionResult New(Customers customers)
    {
       
        return View(customers);
    }

WEB API 控制器

        // create customer
    [HttpPost]
    public IHttpActionResult CreateCustomer(Customers CustomerDTO)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest();
        }
      
        _context.Customerss.Add(CustomerDTO);
        _context.SaveChanges();

        return Created(new Uri(Request.RequestUri + "/" + CustomerDTO.Id), CustomerDTO);

    }

查看

@model API.Models.Customers


@{
    ViewBag.Title = "New";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>New Form</h2>

<div id="container">
    <form>
        <div class="form-group" id="NameGroup">
            @Html.LabelFor(m => m.Name)
            @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        </div>
        <button type="submit" class="btn btn-primary js-Add">Save</button>
    </form>
</div>


@section scripts {

    <script type="text/javascript">
        $(document).ready(function () {
            
            $("#container .js-Add").on("click", function (e) {
                e.preventDefault();

                if (confirm("Are you sure you want to ADD this customer?")) {

                    var input = $("Name").val();

                    $.ajax({
                        url: "../api/Customer",
                        type: "POST",
                        data: JSON.stringify(input),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            alert('HELLO' + response.Name);
                        }
                    });
                }
            });
        });
    </script>
}

感谢您的帮助

【问题讨论】:

  • Customerss的可能类型吗?
  • public DbSet 客户{ get;放; }

标签: jquery asp.net .net asp.net-mvc asp.net-web-api


【解决方案1】:

您有问题。您正在尝试从 id 为 Name 的 DOM 元素中获取值。但是,您没有在任何地方指定它。

@Html.TextBoxFor(m => m.Name, new { @class = "form-control", id = "Name" })

@Html.TextBoxFor(item => item.OperationNo, htmlAttributes: new { id = "Name", class = "form-control" })

更新:

@section scripts {

<script type="text/javascript">
    $(document).ready(function () {
        
        $("#container .js-Add").on("click", function (e) {
            e.preventDefault();

            if (confirm("Are you sure you want to ADD this customer?")) {

                var input = $("Name").val();
                var request = { Name: input };

                $.ajax({
                    url: "../api/Customer",
                    type: "POST",
                    data: JSON.stringify(request),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert('HELLO' + response.Name);
                    }
                });
            }
        });
    });
</script>
}

【讨论】:

  • 谢谢你的帮助,但我还是有同样的问题
  • @patrick 我已经更新了答案,请再次查看
  • 感谢您的努力,我没有收到任何错误,但警报不起作用,也没有数据添加到数据库中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 2018-06-03
  • 2010-11-11
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多