I’m trying to post data to a controller in ASP.NET Core
I receive null in the controller.
I can’t get any data POSTed to an endpoint.
In the action
[HttpPost]
public ActionResult AddEarningg([FromBody] ProgramAddDTO program)
{
//_ProgramAppService.AddEarning(program);
ViewData["tenantlist"] = ListItems();
return View();
}
In the ajax code:
<script src="~/Common/Scripts/select2.min.js"></script>
<script>
$(document).ready(function () {
$("#SubProductCodeId").select2();
$("#ProductId").select2();
$("#ProductCodeId").select2();
$("#ProductCodeId").select2();
$("#CountryId").select2();
$("#MerchantId").select2();
$("#ChannelId").select2();
$("#SegmentId").select2();
$("#BtnSubmit").click(function () {
var formdata = {};
formdata.institutionId = $("#InstitutionId").val();
formdata.programName = $("#ProgramName").val();
formdata.productId = $("#ProductId").val();
formdata.productCodeId = $("#ProductCodeId").val();
formdata.subProductId= $("#SubProductId").val();
formdata.TransactionTypeId= $("#TransactionTypeId").val();
formdata.ChannelId= $("#ChannelId").val();
formdata.SubProductCodeId= $("#SubProductCodeId").val();
formdata.SpendToEarn= $("#SpendToEarn").val();
formdata.EquivalnetPoint= $("#EquivalnetPoint").val();
formdata.CountryId= $("#CountryId").val();
formdata.MCCId= $("#MCCId").val();
formdata.MerchantId= $("#MerchantId").val();
formdata.SegmentId= $("#SegmentId").val();
formdata.CapsMin= $("#CapsMin").val();
formdata.CapsMax= $("#CapsMax").val();
formdata.PointsExpiry = $("#PointsExpiry").val();
var t = JSON.stringify(formdata)
$.ajax({
url: '/app/Program/AddEarningg',
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ 'program': formdata }),
success: function () {
alert("test");
}
});
});
});
</script>
in the DTO
ProgramAddDTO Class
public class ProgramAddDTO
{
//public int? ProgramTypeId { get; set; }
public int InstitutionId { get; set; }
public string ProgramName { get; set; }
public List<int?> ProductId { get; set; }
public List<int?> ProductCodeId { get; set; }
public List<int?> SubProductId { get; set; }
public List<int?> SubProductCodeId { get; set; }
public List<int?> CountryId { get; set; }
public int? TransactionTypeId { get; set; }
public int EquivalnetPoint { get; set; }
public int PointValue { get; set; }
public List<int?> ChannelId { get; set; }
public List<int?> MCCId { get; set; }
public List<int?> MerchantId { get; set; }
public List<int?> SegmentId { get; set; }
public decimal CapsMin { get; set; }
public decimal CapsMax { get; set; }
public int? PointsExpiry { get; set; }
public int SpendToEarn { set; get; }
}
When I pass a complex JSON to the post method, then it always shows me null.
I’m using the select2 jquery library.
2
Answers
its working by me when parse value from form to int
First, don’t use Uppercase letters for your json properties. For example
should become
and so one.
Because by default in .net core asp.net it looks for not capitalized json properties and it convert it to the correspondent property in C# that start with uppercase.
And the request should works simply with: