简体   繁体   English

请求数据未从视图传递到 Controller

[英]Request Data Not Passed From View To Controller

I have a view That Posts data as follows (image from fiddler request) Request Body我有一个发布数据如下的视图(来自提琴手请求的图像)请求正文

and I am accepting it in the action as follows:我在行动中接受它如下:

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 
}

I am testing locally and everything is fine yet the published code gives me a null reference exception when I access the Requireds in the model passed from the view我正在本地测试,一切都很好,但是当我访问从视图传递的 model 中的Requireds 时,发布的代码给了我一个 null 引用异常

my models are like this我的模型是这样的

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

Any Idea Why is the data not passed to the action method only in production?任何想法为什么仅在生产中不将数据传递给操作方法?

without seeing your "View page" I'm guessing you are not binding the request body.没有看到您的“查看页面”,我猜您没有绑定请求正文。 try these changes.试试这些改变。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM