Good Day Community. I will be grad if one can help with this issue. In a master detail form using jquery,json and netcore 5.0 with Microsoft.AspNetCore.Mvc.NewtonsoftJson the json is generated but the controller receive null.
In the javascript seccion:
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');
}
In the controller section:
[HttpPost]
public JsonResult Create(SaleformViewModel quote)
{
bool status = false;
if (ModelState.IsValid)
{
....
//Save
_context.SaveChanges();
status = true;
}
The ViewModel is:
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; }
}
and the Saleitem class is:
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; }
}
Thanks.
CodePudding user response:
Since you are using an 'application/json' content type, you need to add a [FromBody] attribute to your post action
public JsonResult Create([FromBody] SaleformViewModel quote)