发送到mvc控制器时,json数据被接收为null

时间:2016-03-29 16:00:41

标签: javascript c# json asp.net-mvc forms

我正在尝试将表单数据发送到mvc控制器,当用户单击提交表单时,会在提示框中要求输入。需要将这些答案与表单一起提交给控制器。当提交时,JSON数据不会发送到控制器。

形式:

@using (Html.BeginForm("BottomRowData", "Tanks", FormMethod.Post, new { @id = "formBottom"}))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(p => p.TankSerNo)
@Html.HiddenFor(p => p.AnnularPlateRingWidth)
@Html.HiddenFor(p => p.connectedtank)
@Html.HiddenFor(p => p.NumberofAnnularPlates)
@Html.HiddenFor(p => p.LengthofEachAnnularPlate)
<div class="row">
    <div class="col-md-1 col-xs-4">
        <div class="form-group">
            <button type="submit" name="action:Save" class="btn btn-primary">
                <span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> Save
            </button>
        </div>
    </div>

    <div class="col-md-4 col-xs-8">
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(p => p.TankOwner, "Tank Owner", htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-8">
                    @Html.DropDownList("TankOwner", null, new { @class = "form-control" })
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-4 col-xs-6">
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(model => model.connectedtank.SiteID, "Site (Tank Location)", htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-8">
                    @Html.HiddenFor(m => m.connectedtank.SiteID)
                    @Html.EditorFor(model => model.connectedtank.Site.SiteName, new { htmlAttributes = new { @class = "form-control", @readonly = true } })
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-3 col-xs-6">
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(model => model.TankNumber, "Tank Number", htmlAttributes: new { @class = "control-label col-md-5" })
                <div class="col-md-7">
                    @Html.EditorFor(m => m.TankNumber, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.TankNumber, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
    </div>
</div>

<hr style="margin-top:0px" />

<fieldset class="fi-border">
<legend class="fi-border">Bottom Row Data</legend>
<div class="row">
    <div class="col-sm-3 com-xs-12">
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(model => model.NumberofRowsBottom, "Number of Bottom Rows:", htmlAttributes: new { @class = "control-label col-md-6", @style = "padding-top:0px" })
                <div class="col-md-6">
                    @Html.EditorFor(model => model.NumberofRowsBottom, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.NumberofRowsBottom, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
    </div>

    <div class="col-sm-6 com-xs-12">
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(model => model.WeldRowSpacing, "Weld Row Spacing or Plate Width (Comma Seperated):", htmlAttributes: new { @class = "control-label col-md-5", @style = "padding-top:0px" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.WeldRowSpacing, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.WeldRowSpacing, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
    </div>

    <div class="col-xs-12 col-sm-3">
        <button class="btn btn-default btn-block" type="submit" onclick="JavascriptFunction1()" id="BottomButton">
            <strong>1.</strong> Calculate Bottom Row Data
        </button>
    </div>

    <div class="col-sm-12">
        @Html.CollectionEditorFor(model => model.BottomRows, "BottomLayout/_AddRow", "/Tanks/GetRowEditor",
                "Add Rows", new { @style = "display:none" })
    </div>
</div>

}

脚本:

<script type="text/javascript" language="javascript">
function JavascriptFunction1() {
    var url = '@Url.Action("BottomRowData", "Tanks")';
    var NumberofRows = parseFloat($('#NumberofRowsBottom').val());
    var numberofPlates = [];
    $("#divLoading").show();
    var i = 1;
    while (i <= NumberofRows) {
        numberofPlates.push(prompt("Please enter the number of plates on row " + i));
        i++; 
    }
    $.ajax({
        type: "POST",
        url: url,
        traditional: true,
        datatype: "json",
        data: JSON.stringify(numberofPlates),
        success: function(data){
            $("#PID")[0].innerHTML = data;
            $("#divLoading").hide();
        }

    });
}

控制器:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> BottomRowData(BottomLayoutViewModel data, string[] postdata)
    {
        var tank = db.Tanks.Find(data.TankSerNo);
        tank.NumberofRowsBottom = data.NumberofRowsBottom;
        string[] cRowData = new string[1000];
        string BottomWeldData = (tank.Diameter / data.NumberofRowsBottom).ToString();
        for (var i = 1; i <= data.NumberofRowsBottom; i++)
        {
            if(i == 1)
            {
                cRowData[i] = BottomWeldData;
            }
            else
            {
                cRowData[i] = cRowData[1];
                BottomWeldData = BottomWeldData + ", " + cRowData[i];
            }
        }
        tank.WeldRowSpacing = BottomWeldData;
        db.Entry(tank).State = EntityState.Modified;
        await db.SaveChangesAsync();
        var Radius = float.Parse((tank.Diameter / 2).ToString());
        float sumRowData = 0;
        double[] rowLength = new double[1000];
        for (var counter = 1; counter <= data.NumberofRowsBottom; counter++)
        {
            var row = new TankBottomLayout();
            if (data.BottomRows != null)
            {
                row = data.BottomRows.ElementAt(counter - 1);
            }
            sumRowData += float.Parse(cRowData[counter]);
            row.TankSerNo = tank.TankSerNo;
            row.Tank = tank;
            row.BLORowNumber = counter;
            row.BLONumberofPlatesRow = int.Parse(postdata.ElementAt(counter - 1));
            rowLength[counter] = 2 * Math.Pow((Math.Pow(Radius, 2) - Math.Pow((sumRowData - Radius), 2)), 0.5);
            if(counter == 1)
            {
                row.BLOWeldRowLength = rowLength[counter];
            }
            else if(counter < data.NumberofRowsBottom / 2 + 1)
            {
                row.BLOWeldRowLength = rowLength[counter - 1];
            }
            else if(counter == data.NumberofRowsBottom)
            {
                row.BLOWeldRowLength = rowLength[counter - 1];
            }
            else
            {
                row.BLOWeldRowLength = rowLength[counter];
            }

            if(row.ID <= 0)
            {
                db.TankBottomLayouts.Add(row);
            }
            else
            {
                db.Entry(row).State = EntityState.Modified;
            }
        }
        await db.SaveChangesAsync();
        return RedirectToAction("BottomLayout", new { TankSerNo = data.TankSerNo });
    }

我的主要问题是我需要一种方法来询问用户每行上的印版数量,并将该数据发送到控制器以计算每行的焊接间距。谢谢。

1 个答案:

答案 0 :(得分:0)

此行为是由Ajax函数的异步性引起的,请参阅此post获取来自异步函数的值