-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 });
}
我的主要问题是,我需要一种方法来询问用户每行的板数并将该数据发送到控制器,以便计算每行的焊接间距。谢谢。
你能否从开发者工具粘贴请求主体? – sam
一个可能不相关的问题是,当您在表单上包含AntiForgeryToken并且您的POST方法正在检查令牌时,您不包括令牌以及AJAX调用。 –
你是什么意思_the JSON数据没有被发送到controller_?你显示的代码会向控制器发送一个数组或数字,但控制器方法甚至不接受数组 - 它具有参数'BottomLayoutViewModel data'和'string [] postdata',它们与你发送的数据(和如前所述,由于您不包括'AntiForgeryToken',因此无论如何你都会得到'500 Internal Server Error') –