C# 请求数据未从视图传递到控制器
我有一个视图发布如下数据(来自fiddler请求的图像) 我在行动中接受它如下:C# 请求数据未从视图传递到控制器,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个视图发布如下数据(来自fiddler请求的图像) 我在行动中接受它如下: public ActionResult AddProblem(Problem newproblem, string submit) { // Validating Saving the problem in the database // part of it as follows if (ModelState.IsValid)
public ActionResult AddProblem(Problem newproblem, string submit)
{
// Validating Saving the problem in the database
// part of it as follows
if (ModelState.IsValid)
{
decimal TotalValue = 0M;
foreach (var req in newproblem.Requireds) // This Line Throws a Null Reference Only in Production
{
TotalValue += req.Value;
}
// rest of the code
}
我正在本地测试,一切正常,但是当我访问从视图传递的模型中的required时,发布的代码给了我一个null引用异常
我的模特是这样的
public class Problem
{
public Problem()
{
generalInformation = new GeneralQuestion();
Requireds = new List<ProblemRequired>();
}
public GeneralQuestion generalInformation { get; set; }
public List<ProblemRequired> Requireds { get; set; }
public int Id { get; set; }
[Display(Name = "Problem Text")]
[Required]
public string Text { get; set; }
public decimal TotalMark { get; set; }
}
public class ProblemRequired
{
[Required]
public string Text { get; set; }
public string Status { get; set; }
[Required]
public string SolutionKey { get; set; }
public int Order { get; set; }
[Required]
[Display(Name = "Question Mark")]
[Range(0.01, 100.00, ErrorMessage = "Please enter a correct mark")]
public decimal Value { get; set; }
[Required]
public decimal CorrectAnswer { get; set; }
[Required]
public int CorrectNotation { get; set; }
[Required]
public string Unit { get; set; }
}
公共类问题
{
公共问题()
{
一般信息=新的一般问题();
Requireds=新列表();
}
公共一般问题一般信息{get;set;}
所需的公共列表{get;set;}
公共int Id{get;set;}
[显示(Name=“问题文本”)]
[必需]
公共字符串文本{get;set;}
公共十进制TotalMark{get;set;}
}
需要公共类问题
{
[必需]
公共字符串文本{get;set;}
公共字符串状态{get;set;}
[必需]
公共字符串解决方案密钥{get;set;}
公共整数顺序{get;set;}
[必需]
[显示(Name=“问号”)]
[范围(0.01、100.00、ErrorMessage=“请输入正确的标记”)]
公共十进制值{get;set;}
[必需]
公共十进制正确答案{get;set;}
[必需]
公共整数更正符号{get;set;}
[必需]
公共字符串单元{get;set;}
}
你知道为什么只有在生产环境中数据才没有传递到action方法吗?没有看到你的“查看页面”,我猜你没有绑定请求主体。试试这些改变
[HttpPost, ActionName("AddProblem")]
public ActionResult AddProblem([FromBody]Problem newproblem, string submit)
你有什么错误?您是否检查了浏览器开发人员工具?在将自定义错误模式关闭后,我在上面提到的关于开发人员工具的行中出现了NullReferenceException。没有什么是不常见的,但图片显示requires不为null。我认为新问题是空的。你签入调试器了吗?在本地一切都是正确的,而在服务器上它给出了错误TotalValue呢?我不认为它是指定的