“控制器、动作、模型"并非所有代码路径都返回值 [英] "Controller, Action, Model" not all code paths return a value

查看:17
本文介绍了“控制器、动作、模型"并非所有代码路径都返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的被这个错误不是所有的代码路径都返回一个值"搞糊涂了.在我的行动 PostResponse 上.我已经盯着我的模型、控制器和视图看了几个小时,我想我已经涵盖了所有路径.当然项目不会构建,所以我无法进一步调试.

我的行动

//POST:/Questions/ViewQuestion/5[HttpPost][验证AntiForgeryToken]public ActionResult PostResponse([Bind(Include = "UserId,QuestionID,Answer,Source,Status,DateStamp")] 响应响应){如果(模型状态.IsValid){db.Responses.Add(response);db.SaveChanges();}}

我的观点

@model Template.Models.Question@using Microsoft.AspNet.Identity@{ViewBag.Title = "查看问题";var qtype = Model.QuestionTypeId;ViewBag.Number = Model.Id - 7;}@using (Html.BeginForm("Question", "ViewQuestion", FormMethod.Post, new { @class = "form-horizo​​ntal", role = "form" }))<div><h4>问题#@ViewBag.Number</h4><小时/><h1>@Model.Question1</h1>

<div class="form-group">@switch (qtype){情况1://文本框@Html.TextArea("Answer", new { @class = "form-control", rows = "4", col = "5" });休息;案例2:// 落下<select class="form-control";id="答案">@foreach(Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking) 中的变量项目){<选项值=@item.QuestionOption1">@item.QuestionOption1</option>}</选择>休息;案例3://复选框<div class="checkbox">@foreach(Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking) 中的变量项目){<输入类型=复选框"名称=答案"值=@item.QuestionOption1";/>@item.QuestionOption1
}

休息;案例4://单选按钮foreach(Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking) 中的变量项){<div class="radio"><标签><输入类型=收音机"名称=答案"值=@item.QuestionOption1";/>@item.QuestionOption1

}休息;}

@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"><输入类型=提交"class="btn btn-default";值=答案"/>

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

页面显示完美,但我无法测试发布操作,因为我无法使用当前错误构建.

解决方案

解决了;但它几乎是从头开始的,因为我创建了一个新的 ViewModel 并使用它来填充响应.

 [HttpPost]public ActionResult ViewQuestion([Bind(Include = "QuestionId, Answer, UserId")] ResponseViewModel responseViewModel){响应 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");}

感谢您的意见,因为您的意见让老头再次工作.谢谢!

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.

解决方案

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屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆