gpt4 book ai didi

jquery - "The input was not valid".NET Core Web API 与 Razor 页面

转载 作者:行者123 更新时间:2023-12-01 06:43:27 25 4
gpt4 key购买 nike

任何人都可以帮忙,当我在 postman 中使用 post 方法时,我只得到“输入无效”。请帮忙,我在这上面花了太多时间!!!

Controller :

[HttpPost]
public IActionResult Create(M05PurchaseAgreement item)
{
_context.M05PurchaseAgreement.Add(item);
_context.SaveChanges();

return CreatedAtRoute("GetTodo", new { agreementNo = item.AgreementNo }, item);
}

型号:

public class M05PurchaseAgreement
{
[Key]
public string AgreementNo { set; get; }
public string Status { set; get; }
public string OrderStatus { set; get; }
public string AccountStatus { set; get; }
public string StockID { set; get; }
public string RegistrationNo { set; get; }
public string Revision { set; get; }
public string CalendarPeriod { set; get; }
public string PurchaseType { set; get; }
public DateTime PurchaseDate { set; get; }
public string PurchaseSalesman { set; get; }
public decimal PurchasePrice { set; get; }
public string TaxMethod { set; get; }
public string PurchaseInvoice { set; get; }
public string EntryStaff { set; get; }
public DateTime EntryDate { set; get; }
public string LastEditStaff { set; get; }
public DateTime LastEditDate { set; get; }
public string Notes { set; get; }
public string BranchCode { set; get; }
public string Location { set; get; }
}

编辑 - 我使用 Jquery

Jquery:

$.ajax({
type: 'POST',
url: 'https://localhost:44328/api/m05purchaseagreement',
accepts: 'application/json',
contentType: 'application/json',
data: JSON.stringify(item),
error: function (jqXHR, textStatus, errorThrown) {
alert(JSON.stringify(item));
},
success: function (result) {
$('#agreementNo').val('');
}
});

最佳答案

如果您使用[ApiController],模型验证错误会自动触发 HTTP 400 响应。您可以通过将 SuppressModelStateInvalidFilter 属性设置为 true 来禁用默认行为。请参阅文档 here .

在 Postman 中,我使用原始 json 发布模型数据,如下所示。

enter image description here

在 Controller 中,使用[FromBody]

[HttpPost]
public IActionResult Create([FromBody] M05PurchaseAgreement item)

而在view中,你需要传递json数据,你的item类似于

var item = { "Status": "active", "OrderStatus": "active" };

关于jquery - "The input was not valid".NET Core Web API 与 Razor 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53631249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com