Alex
Alex

Reputation: 121

"Controller, Action, Model" not all code paths return a value

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 I have all paths covered. Of course the project won't build, so I can't debug further.

My action

    // POST: /Questions/ViewQuestion/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult PostResponse([Bind(Include = "UserId,QuestionID,Answer,Source,Status,DateStamp")] Response response)
    {
        if (ModelState.IsValid)
        {
            db.Responses.Add(response);
            db.SaveChanges();
        }
    }

My view

@model Template.Models.Question
@using Microsoft.AspNet.Identity
@{
   ViewBag.Title = "View question";
    var qtype = Model.QuestionTypeId;
    ViewBag.Number = Model.Id - 7;
    
}
@using (Html.BeginForm("Question", "ViewQuestion", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
<div>
    <h4>Question #@ViewBag.Number</h4>
    <hr />
    <h1> @Model.Question1</h1>
</div>

<div class="form-group">
    @switch (qtype)
    {
        case 1:
        // Textbox
            @Html.TextArea("Answer", new { @class = "form-control", rows = "4", col = "5" });
            break;
        case 2:
        //      Dropdown
        <select class="form-control" id="Answer">
            @foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
            {
                <option value="@item.QuestionOption1">@item.QuestionOption1</option>

            }
        </select>
            break;
        case 3:
        //     Checkbox
        <div class="checkbox">
                    @foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
{
    <input type="checkbox" name="Answer" value="@item.QuestionOption1" />  @item.QuestionOption1 <br />
                    }
        </div>
        break;

        case 4:
        //      Radio buttons
        foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
            {
                <div class="radio">
                    <label>
                        <input type="radio" name="Answer" value="@item.QuestionOption1" />
                        @item.QuestionOption1
                    </label>
                </div>
            }

            break;
    }

</div>
@using Template.Models.Response
@Html.HiddenFor(r => r.Responses, new { UserId = User.Identity.GetUserId(), Source = "Web", Status = "New", DateStamp = System.DateTime.Now })
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Answer" />
    </div>
</div>
<br />
<hr />
<p>

    @Html.ActionLink("Previous", "ViewQuestion", new { id = Model.Id - 1 }) |
    @Html.ActionLink("Next", "ViewQuestion", new { id = Model.Id + 1 })
</p>

The page displays perfectly, but I can't test the post action as I cannot build with the current error.

Upvotes: 0

Views: 4021

Answers (3)

Alex
Alex

Reputation: 121

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!

Upvotes: 2

Namila
Namila

Reputation: 754

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

}

Upvotes: 0

Kevin
Kevin

Reputation: 429

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

Upvotes: 0

Related Questions