2016-03-29 63 views
-1

我試圖發送表單數據到mvc控制器,當用戶單擊提交表單時,他們被要求在提示框中輸入。這些答案需要以表格的形式提交給管理員。當提交時,JSON數據不會被髮送到控制器。當發送給mvc控制器時,json數據被接收爲null

形式:

@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

你能否從開發者工具粘貼請求主體? – sam

+0

一個可能不相關的問題是,當您在表單上包含AntiForgeryToken並且您的POST方法正在檢查令牌時,您不包括令牌以及AJAX調用。 –

+0

你是什麼意思_the JSON數據沒有被髮送到controller_?你顯示的代碼會向控制器發送一個數組或數字,但控制器方法甚至不接受數組 - 它具有參數'BottomLayoutViewModel data'和'string [] postdata',它們與你發送的數據(和如前所述,由於您不包括'AntiForgeryToken',因此無論如何你都會得到'500 Internal Server Error') –

回答

0

此行爲是由於Ajax的功能Asynchronosity,請閱讀本post從異步函數獲取值

相關問題