ASP 网络核心 AJAX Post 向控制器发送 NULL
ASP Net Core AJAX Post Sending NULL to Controller
我有问题
我尝试 post 数据到 asp 核心中的控制器
我在控制器中修改此数据为空
我无法将任何数据 POST 发送到端点。
在行动中
[HttpPost]
public ActionResult AddEarningg([FromBody] ProgramAddDTO program)
{
//_ProgramAppService.AddEarning(program);
ViewData["tenantlist"] = ListItems();
return View();
}
在ajax这段代码
<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>
在 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; }
}
当我将复杂的 JSON 传递给 post 方法时,它总是显示为 null。
我使用 2select jquery 库
首先,不要在 json 属性中使用大写字母。例如
formdata.TransactionTypeId
应该变成
formdata.transactionTypeId
等等。
因为默认情况下,在 .net 核心 asp.net 中,它会查找未大写的 json 属性,并将其转换为 C# 中以大写字母开头的相应 属性。
并且该请求应该仅适用于:
$.ajax({
url: '/app/Program/AddEarningg',
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(formdata) ,
success: function () {
alert("test");
}
当我将值从表单解析为 int 时,它由我来工作
var pinstitutionId =parseInt( $("#InstitutionId").val());
var pprogramName = $("#ProgramName").val();
var pproductId = $("#ProductId").val();
var pproductCodeId = $("#ProductCodeId").val();
var psubProductId= $("#SubProductId").val();
var ptransactionTypeId= parseInt($("#TransactionTypeId").val());
var pchannelId= $("#ChannelId").val();
var psubProductCodeId= $("#SubProductCodeId").val();
var pspendToEarn= parseInt($("#SpendToEarn").val());
var pequivalnetPoint= parseInt($("#EquivalnetPoint").val());
var pcountryId= $("#CountryId").val();
var pmCCId= $("#MCCId").val();
var pmerchantId= $("#MerchantId").val();
var psegmentId= $("#SegmentId").val();
var pcapsMin= parseInt($("#CapsMin").val());
var pcapsMax= parseInt($("#CapsMax").val());
var ppointsExpiry =parseInt( $("#PointsExpiry").val()) ;
我有问题 我尝试 post 数据到 asp 核心中的控制器 我在控制器中修改此数据为空
我无法将任何数据 POST 发送到端点。
在行动中
[HttpPost]
public ActionResult AddEarningg([FromBody] ProgramAddDTO program)
{
//_ProgramAppService.AddEarning(program);
ViewData["tenantlist"] = ListItems();
return View();
}
在ajax这段代码
<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>
在 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; }
}
当我将复杂的 JSON 传递给 post 方法时,它总是显示为 null。
我使用 2select jquery 库
首先,不要在 json 属性中使用大写字母。例如
formdata.TransactionTypeId
应该变成
formdata.transactionTypeId
等等。
因为默认情况下,在 .net 核心 asp.net 中,它会查找未大写的 json 属性,并将其转换为 C# 中以大写字母开头的相应 属性。
并且该请求应该仅适用于:
$.ajax({
url: '/app/Program/AddEarningg',
type: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(formdata) ,
success: function () {
alert("test");
}
当我将值从表单解析为 int 时,它由我来工作
var pinstitutionId =parseInt( $("#InstitutionId").val());
var pprogramName = $("#ProgramName").val();
var pproductId = $("#ProductId").val();
var pproductCodeId = $("#ProductCodeId").val();
var psubProductId= $("#SubProductId").val();
var ptransactionTypeId= parseInt($("#TransactionTypeId").val());
var pchannelId= $("#ChannelId").val();
var psubProductCodeId= $("#SubProductCodeId").val();
var pspendToEarn= parseInt($("#SpendToEarn").val());
var pequivalnetPoint= parseInt($("#EquivalnetPoint").val());
var pcountryId= $("#CountryId").val();
var pmCCId= $("#MCCId").val();
var pmerchantId= $("#MerchantId").val();
var psegmentId= $("#SegmentId").val();
var pcapsMin= parseInt($("#CapsMin").val());
var pcapsMax= parseInt($("#CapsMax").val());
var ppointsExpiry =parseInt( $("#PointsExpiry").val()) ;