0

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.

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Tried this but it does not populate the DB. Also after the post it does not automatically return to the same screen, in fact it breaks completely. So I tried adding; return RedirectToAction("/ViewQuestion/" + (response.QuestionId + 1) ); I put back ActionResult in place of void but now I'm getting the same error again. ??
Well, there's a couple different requirements you have now. I can't speak to why your item isn't saved in the database. Maybe your ModelState isn't valid and it never executed this block. If you want to return to the 'same' screen you're going to need to return View(response). This will return you to the View for that Action and by passing a model your view can be re-bound to it.
0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.