我对我的操作PostResponse上的错误“并非所有代码路径都返回值”感到困惑。我已经盯着我的模型,控制器和视图几个小时了,我想我已经覆盖了所有路径..当然项目不会构建,所以我无法进一步调试..
我的行动;
// 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>
页面显示完美,但我无法测试后期操作,因为我无法使用当前错误进行构建。
感谢您的帮助。
答案 0 :(得分:2)
解决了;但它几乎是从零开始,因为我创建了一个新的ViewModel并使用它来填充响应。
[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");
}
感谢您的意见,因为您的评论让老头再次工作。谢谢!
答案 1 :(得分:0)
您的PostResponse操作或方法将ActionResult指定为返回类型,但实际上并未返回任何内容。您可以通过将其从ActionResult更改为void
来解决此问题答案 2 :(得分:0)
试
[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");
}
}