美好的一天社區。如果有人可以幫助解決這個問題,我將成為畢業生。在使用 jquery、json 和 netcore 5.0 以及 Microsoft.AspNetCore.Mvc.NewtonsoftJson 的主詳細資訊表單中,生成了 json,但控制器接收到 null。
在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;
}
視圖模型是:
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; }
}
Saleitem 類是:
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; }
}
謝謝。
uj5u.com熱心網友回復:
由于您使用的是“應用程式/json”內容型別,因此您需要將 [FromBody] 屬性添加到您的帖子操作中
public JsonResult Create([FromBody] SaleformViewModel quote)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/483899.html
