“Controller, Action, Model” not all code paths return a value

前端 未结 3 453
生来不讨喜
生来不讨喜 2021-01-28 12:45

I am really confused by this error "not all code paths return a value" on my action PostResponse. I have stared at my model, controller and view for hours and I think

相关标签:
3条回答
  • 2021-01-28 13:15

    Worked it out; but it was almost from scratch, as I created a new ViewModel and used that to populate the responses.

            [HttpPost]
        public ActionResult ViewQuestion([Bind(Include = "QuestionId, Answer, UserId")] ResponseViewModel responseViewModel)
        {
    
            Response re = new Models.Response();
                re.Answer = responseViewModel.Answer;
                re.UserId = responseViewModel.UserId;
                re.QuestionId = responseViewModel.QuestionId;
                re.DateStamp = System.DateTime.Now;
                db.Responses.Add(re);
                db.SaveChanges();
    
            return RedirectToAction("ViewQuestion");
        }
    

    Thanks for your input as your comments got the old head working again. Thanks!

    0 讨论(0)
  • 2021-01-28 13:21

    try

       [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult PostResponse([Bind(Include = "UserId,QuestionID,Answer,Source,Status,DateStamp")] Response response)
    {
        if (ModelState.IsValid)
        {
            db.Responses.Add(response);
            db.SaveChanges();
        }
        else{
            return View("Error");
        }
    
    }
    
    0 讨论(0)
  • 2021-01-28 13:23

    Your PostResponse action, or method specifies an ActionResult as a return type, but does not actually return anything. You can resolve this by changing it from ActionResult to void

    0 讨论(0)
提交回复
热议问题