首页 > 解决方案 > 请求数据未从视图传递到控制器

问题描述

我有一个发布数据如下的视图(来自提琴手请求的图像) 请求正文

我在以下行动中接受它:

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; }
    }

任何想法为什么仅在生产中不将数据传递给操作方法?

标签: c#asp.net-mvc

解决方案


没有看到您的“查看页面”,我猜您没有绑定请求正文。试试这些改变。

[HttpPost, ActionName("AddProblem")]
public ActionResult AddProblem([FromBody]Problem newproblem, string submit)

推荐阅读