c# - 请求数据未从视图传递到控制器
问题描述
我有一个发布数据如下的视图(来自提琴手请求的图像) 请求正文
我在以下行动中接受它:
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
}
我正在本地进行测试,一切都很好,但是当我访问从视图传递的模型中的Requireds时,发布的代码给了我一个空引用异常
我的模型是这样的
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; }
}
任何想法为什么仅在生产中不将数据传递给操作方法?
解决方案
没有看到您的“查看页面”,我猜您没有绑定请求正文。试试这些改变。
[HttpPost, ActionName("AddProblem")]
public ActionResult AddProblem([FromBody]Problem newproblem, string submit)
推荐阅读
- json - 你如何在 Swift 中解析递归 JSON?
- gitlab - 为 PostgreSQL RDS 实例安装 pg_trgm 扩展
- r - 在 ggplot 和 R 的甘特图中创建一致的条形颜色
- html - 如何根据某些屏幕尺寸传递某些道具
- polymer-3.x - 将函数传递给孩子
- android - 如何在没有太多代码更改的情况下使相对布局应用程序响应?
- javascript - 用户签到位置基于从哪里开始?
- python - 修改“max_depth”时,如何将 DecisionTreeRegressor 的结果保存到不同的数组?
- python - 错误的 Word() 字符串给出了误导性的错误位置
- r - 使用 dplyr 中的“包含”使用多列创建指标变量