MVC C#-GET请求正在工作,POST给我一个错误。我该如何调试?

时间:2019-02-19 02:05:00

标签: c# post model-view-controller

我正在使用针对Web应用程序的ASP.NET MVC入门课程,并且在API部分遇到了一些问题。我的GET请求(使用Postman)正在以JSON格式提取数据,但是我的POST请求却给了我一个无效的ModelState错误。我不确定模型中的哪些内容无效,因此正在寻求有关调试的帮助或指导

我来自邮递员的GET请求吐出数据库中的客户。     {         “ Id”:1         “名称”:“吉米史蒂夫”,         “ IsSubscribedToNewsleter”:是的,         “ MembershipType”:null,         “ MembershipTypeId”:4         “客户生日”:“ 2018-01-01T00:00:00”     }

然后我复制上面的内容,删除ID部分并更改Name并提交为POST请求,然后从ModelState无效错误中收到400错误的请求错误。

我不确定我的模型中有什么问题不允许我发布。

这是控制器代码:

   //GET
    public IEnumerable<Customer> GetCustomers()
    {
        return _context.Customers.ToList();
    }


     //Post
    [HttpPost]
    public Customer CreateCustomer(Customer customer)

    {
        if (!ModelState.IsValid)
            throw new HttpResponseException(HttpStatusCode.BadRequest);

        _context.Customers.Add(customer);
        _context.SaveChanges();

        return customer;
    }

这是型号代码:

    public class Customer


{
    public int Id { get; set; }

    [Required]
    [StringLength(255)]
    public string Name { get; set; }

    public bool IsSubscribedToNewsleter { get; set; }

    public MembershipType MembershipType { get; set; }

    [Display(Name = "Membership Type")]
    public byte MembershipTypeId { get; set; }


    [Display(Name = "Date of Birth")]
    [Min18YearsIfAMember]
    public DateTime? CustomerBirthday { get; set;}

}

这是表单代码:

<h2>Create Customer</h2>

@using (Html.BeginForm("Save", "Customers"))
{
    @Html.ValidationSummary(false)

    <div class="form-group">
        @Html.LabelFor(m => m.Customer.Name)
        @Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Customer.Name)
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.Customer.MembershipTypeId)
        @Html.DropDownListFor(m => m.Customer.MembershipTypeId, new SelectList(Model.MembershipTypes, "Id", "Name"), "", new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Customer.MembershipTypeId)
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.Customer.CustomerBirthday)
        @Html.TextBoxFor(m => m.Customer.CustomerBirthday, "{0:d MMM yyyy}", new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Customer.CustomerBirthday)
    </div>

    <div class="checkbox">
        <label>
            @Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsleter) Subscribed to Newsletter
        </label>
    </div>

    @Html.HiddenFor(m => m.Customer.Id)
    @Html.AntiForgeryToken()

    <button type="submit" class="btn btn-primary"> Save </button>
}

我不确定自己在做什么错。我觉得这与必填字段有关-但是我正在提交请求中的所有字段,所以不确定我要去哪里。任何有关调试的想法或技巧将不胜感激!

编辑:在下面保存代码

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(Customer customer)
{
    if (!ModelState.IsValid)
    {
        var viewModel = new CustomerFormViewModel
        {
            Customer = customer,
            MembershipTypes = _context.MembershipTypes.ToList()
        };

        return View("CustomerForm", viewModel);
    }

    if (customer.Id == 0)
        _context.Customers.Add(customer);

    else
    {
        var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);

        customerInDb.Name = customer.Name;
        customerInDb.CustomerBirthday = customer.CustomerBirthday;
        customerInDb.MembershipTypeId = customer.MembershipTypeId;
        customerInDb.IsSubscribedToNewsleter = customer.IsSubscribedToNewsleter;
    }

    _context.SaveChanges();

    return RedirectToAction("Index", "Customers");
}

0 个答案:

没有答案