首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >控制器中的System.NullReferenceException ASP.Net MVC

控制器中的System.NullReferenceException ASP.Net MVC
EN

Stack Overflow用户
提问于 2016-01-31 14:07:07
回答 1查看 249关注 0票数 2

工作(简单保龄球游戏网络应用),其中有两个文本框和一个提交按钮。

文本框1= FirstRoll,文本框2= SecondRoll

用户输入两个数字,然后显示一个分数。

发布:在提交操作方法中获取System.NullReferenceException。为什么一开始帧是空的?

控制器

代码语言:javascript
复制
    [HttpPost]
    public JsonResult Submit(Frame[] _frames)
    {
        int result= 0;
        var objBowlingScore = new GameEngineService();

        foreach(var frame in _frames)
        {
            result = objBowlingScore.CalculateFrameScore(frame);
        }

        return Json(objBowlingScore);
    }

为了提高代码可读性,删除了模型服务器端检查验证。

代码语言:javascript
复制
public class Frame
{
    public int FrameId { get; set; }
    public int FirstRoll { get; set; }
    public int SecondRoll { get; set; }
    public int ThirdRoll { get; set; }
    public int Score { get; set; }

}

视图

代码语言:javascript
复制
<p>1st Roll: @Html.EditorFor(m => m.FirstRoll)</p>
<p>2nd Roll: @Html.EditorFor(m => m.SecondRoll)</p>

<button id="submitButton" class="btn btn-primary btn-lg">Submit Score</button>

<p><label>The current frame is : </label><label id="lblFrameCount"></label></p>
<p><label>The current score is : </label><label id="lblTotalScore"></label></p>

@section DocumentReady {
<script type="text/javascript">

        var bowlingData = { "frames": [] };
        $('#submitButton').click(function (e) {
            var temp = {
                "firstroll": $("#FirstRoll").val(),
                "secondroll": $("#SecondRoll").val()
            };

            bowlingData.frames.push(temp);
            var element = this;

            $.ajax({
                url: "/Home/Submit",
                type: "POST",
                data: JSON.stringify(bowlingData),
                dataType: "json",
                traditional: true,
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    $("#lblTotalScore").text(data.Score);
                    $("#FirstRoll").val("");
                    $("#SecondRoll").val("");
                },
                error: function () {
                    alert("An error has occured!!!");
                }
            });
        });

</script>

服务

代码语言:javascript
复制
    ....
    public int CalculateFrameScore(Frame _frame)
    {
        return _frame.FirstRoll + _frame.SecondRoll + _frame.ThirdRoll;
    }

    ....
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-31 14:37:34

问题是提交方法中的参数名称与jQuery事件处理程序中使用的参数不匹配。

_frames应该是frames。(即:

曾经是

公共JsonResult提交(Frame[]_frames)

应该是

公共JsonResult提交(Frame[]帧)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35114741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档