C# Post请求为空(ASP.NET核心)
我想发送post请求并将数据添加到数据库表中 这是我的型号:C# Post请求为空(ASP.NET核心),c#,asp.net,asp.net-mvc,post,C#,Asp.net,Asp.net Mvc,Post,我想发送post请求并将数据添加到数据库表中 这是我的型号: public partial class PaymentMethods { public PaymentMethods() { PaymentToUser = new HashSet<PaymentToUser>(); } public int Id { get; set; } public int? CardNumber { get; set; } pub
public partial class PaymentMethods
{
public PaymentMethods()
{
PaymentToUser = new HashSet<PaymentToUser>();
}
public int Id { get; set; }
public int? CardNumber { get; set; }
public int? Month { get; set; }
public int? Year { get; set; }
public int? Cvv { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Index { get; set; }
public string Country { get; set; }
public ICollection<PaymentToUser> PaymentToUser { get; set; }
}
[HttpPost]
public JsonResult AddPaymentMethod(PaymentMethods payment)
{
string result;
if(ModelState.IsValid)
{
_context.PaymentMethods.Add(payment);
_context.SaveChanges();
result = "Added";
}
else
{
result = "Error";
}
return Json(result);
}
这是我通过邮递员发送的JSON:
public partial class PaymentMethods
{
public PaymentMethods()
{
PaymentToUser = new HashSet<PaymentToUser>();
}
public int Id { get; set; }
public int? CardNumber { get; set; }
public int? Month { get; set; }
public int? Year { get; set; }
public int? Cvv { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Index { get; set; }
public string Country { get; set; }
public ICollection<PaymentToUser> PaymentToUser { get; set; }
}
[HttpPost]
public JsonResult AddPaymentMethod(PaymentMethods payment)
{
string result;
if(ModelState.IsValid)
{
_context.PaymentMethods.Add(payment);
_context.SaveChanges();
result = "Added";
}
else
{
result = "Error";
}
return Json(result);
}
{
“卡号”:2345678912343456,
“月”:10,
“年份”:20,
“CVV”:322,
“姓名”:“尤金”,
“姓氏”:“苏霍姆林”,
“指数”:83050,
“国家”:“UA”
}
因此,我认为所有数据都很好,但我在文章中得到了controller方法中的空对象,我的错误在哪里?如果您从body传递json,请尝试将
[FromBody]
放在方法参数中。230亿美元肯定不适合int
。我怀疑信用卡号应该是字符串,不是吗?你曾经需要对信用卡号进行数学运算吗?谢谢;s表示建议@davidb,但它对DB中的空对象没有帮助@David@David你不能直接用它来做数学题。但您可能会惊讶地发现,许多信用卡号都实现了a。我在医疗保健行业研究医疗提供者编号时发现了这个算法,并认为它非常简洁。@EugeneSukh您需要在如下输入参数之前添加[FromBody]
:public JsonResult AddPaymentMethod([FromBody]PaymentMethods payment)
这实际上是一个评论,而不是一个答案。我看到你是新来的堆栈溢出,你可以评论,一旦你收到50的声誉。它不工作无论如何:)