“控制器,动作,模型";并非所有代码路径都返回值 [英] "Controller, Action, Model" not all code paths return a value
问题描述
我对这个错误感到困惑,并非所有代码路径都返回一个值".在我的动作PostResponse上.我盯着我的模型,控制器和视图看了几个小时,我认为我已经涵盖了所有路径.当然,该项目无法构建,因此我无法进一步调试.
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.
我的动作
// 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();
}
}
我的观点
@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.
推荐答案
解决了;但是这几乎是从头开始的,因为我创建了一个新的ViewModel并使用它来填充响应.
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!
这篇关于“控制器,动作,模型";并非所有代码路径都返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!