json 故障的原因可能是什么?

What can be the reason for json mal function?

美好的一天社区。如果有人可以帮助解决这个问题,我将毕业。 在使用 jquery、json 和 netcore 5.0 的主从表单中 使用 Microsoft.AspNetCore.Mvc.NewtonsoftJson 生成 json 但控制器接收到空值。

在 javascript 部分:

var saleformdetail =
            {
                SaleformId: '0',
                ProductId: $('.selectprod', this).val(),
                //Description: $('.description', this).val(),
                Description: "",
                Quantity: $('.cquantity', this).val(),
                Price: $('.cprice', this).val(),                   
                Discount: $('.cdiscount', this).val(),
                Amount: $('.camount', this).val(),
                CategoryId: $('.selectcat', this).val(),
                field9: "",
                field10: ""
            }
            list.push(saleformdetail);
if (isAllValid) {
        var data = {
            SaleaccountId: $('#saleaccount').val(),
            LeadsourceId: $('#leadsource').val(),
            SalestageId: $('#salestage').val(),
            Name: $('#name').val(),
            Dateup: $('#dateup').val(),                
            LikeLy: $('#likely').val(),                
            Saleitems: list
        }
        $(this).val('Wait...');
        data = JSON.stringify(data);
        alert(data);
        $.ajax({
            type: 'POST',
            url: '/Saleform/Create',
            data: data,
            contentType: 'application/json',
            dataType: 'json',               
            success: function (data) {
                if (data.status) {
                    alert('Correct Save');
                    list = [];
                    $('#name,#dateup,#likely').val('');                        
                    $('#saleformdetailsitems').empty();
                    window.location.href = "/Saleform/Index";
                }
                else {
                    alert('Error');
                }
                $('#create').val('Save');
            },
            error: function (error) {
                console.log(error);
                $('#create').val('Save');
            }
        });
    }
    else {
        alert('Sending Errors');

    }

在控制器部分:

[HttpPost]
    public JsonResult Create(SaleformViewModel quote)
    {            
        

        bool status = false;
        if (ModelState.IsValid)
        {
                ....
                //Save
                _context.SaveChanges();
                status = true;
  

        }

ViewModel 是:

public class SaleformViewModel
{
    [ForeignKey("Saleaccount")]
    [Display(Name = "Sale Account")]
    public int SaleaccountId { get; set; }

    [ForeignKey("Leadsource")]
    public int LeadsourceId { get; set; }

    [ForeignKey("Salestage")]
    public int SalestageId { get; set; }

    [Required(ErrorMessage = "Missing Name")]
    [StringLength(100)]
    public string Name { get; set; }

    [Required(ErrorMessage = "Missing Estimate")]
    [DataType(DataType.Currency, ErrorMessage = "Incorrect Value")]
    public decimal Likely { get; set; }

    [Required(ErrorMessage = "Missing Dateup")]
    [DataType(DataType.Date, ErrorMessage = "Incorrect Value")]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime Dateup { get; set; }

    public List<Saleitem> Saleitems { get; set; }

}

并且销售项目 class 是:

public class Saleitem
{
    [Key]
    public int Id { get; set; }

    [ForeignKey("Saleform")]
    public int SaleformId { get; set; }

    [ForeignKey("Product")]
    public int ProductId { get; set; }

    [StringLength(50)]
    public string Description { get; set; }

    [Required(ErrorMessage = "Missing Quantity")]
    [DataType(DataType.Currency, ErrorMessage = "Incorrect Value")]
    public int Quantity { get; set; }

    [Required(ErrorMessage = "Missing Price")]
    [DataType(DataType.Currency, ErrorMessage = "Incorrect Value")]
    public decimal Price { get; set; }

    [DataType(DataType.Currency, ErrorMessage = "Incorrect Value")]
    public decimal Discount { get; set; }

    [Required]
    [DataType(DataType.Currency, ErrorMessage = "Incorrect Value")]
    public decimal Amount { get; set; }

    public int CategoryId { get; set; }

    public virtual Saleform Saleform { get; set; }

    public virtual Product Product { get; set; }
}

谢谢。

由于您使用的是 'application/json' 内容类型,因此您需要将 [FromBody] 属性添加到 post 操作中

public JsonResult Create([FromBody] SaleformViewModel quote)