以下是我的代码,当我将数据传递到控制器时,所有数据都正确传递,但数组传递为空。请帮忙。 这是我的模型类,在模型中,我有详细信息类列表以访问详细信息记录。 在MVC中,如果所有有效的代码都传递给控制器,则此处的JavaScript代码会产生错误 请尽快提供帮助
型号代码为 公共类ClsFoodItem { public int ItemSysSeq {获取;组; }
[Display(Name = "Item Code")]
//[StringLength(30, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
public string ItemCode { get; set; }
[Display(Name = "Item Name")]
//[StringLength(30, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
public string ItemName { get; set; }
[Display(Name = "Description")]
//[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
public string ItemDescr { get; set; }
//[StringLength(15, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 11)]
public string PurchaseUnit { get; set; }
//[StringLength(15, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
public int UnitPrice { get; set; }
//[StringLength(15, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
public int BoxPrice { get; set; }
//[StringLength(15, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 11)]
public int UnitQtyinBox { get; set; }
public string ItemPicture { get; set; }
public string Remarks { get; set; }
public List<clsFoodItemsInStock> ItemInStock { get; set; }
}
// javascript代码
if(isAllValid){
var data = {
ItemCode : $('#ItemCode').val().trim(),
itemname: $('#ItemName').val().trim(),
ItemDescr: $('#Itemdesp').val().trim(),
purchaseUnit: $('#purchaseUnit').val().trim(),
UnitPrice: $('#UnitPrice').val().trim(),
unitQtyInBox: $('#unitQtyInBox').val().trim(),
boxprice: $('#boxprice').val().trim(),
remarks: $('#remarks').val().trim(),
//mageData: $('#File1').val().trim(),
ItemInStock: stockvalues
}
$(this).val('Please wait...');
$.ajax({
type: "POST",
url: '/FoodItem/Create/',
data: JSON.stringify(data),
dataType: 'JSON',
contentType: "application/json",
success: function (data) {
alert("array: " + stockvalues.join(', '));
//check is successfully save to database
if (data.status == true) {
//will send status from server side
alert('Successfully done.');
//clear form
stockvalues = [];
$('#ItemCode,#ItemName,#Itemdesp,#purchaseUnit,#UnitPrice,#unitQtyInBox,#boxprice,#remarks,#File1').val('');
$('#stockdetailitems').empty();
}
else {
alert('Error');
}
$('#submit').text('Save');
},
//控制器代码
数据库中的路径
//}
using (SqlConnection sqlCon = new SqlConnection(obj_conn.Conn()))
{
sqlCon.Open();
string query = "INSERT INTO [dbo].[FoodItem] (ItemCode,ItemName,ItemDescr,PurchaseUnit,UnitPrice,BoxPrice,UnitQtyinBox,ItemPicture,Remarks) ";
query = query + "OUTPUT inserted .itemSysSeq Values(@ItemCode,@ItemName,@ItemDescr,@PurchaseUnit,@UnitPrice,@BoxPrice,@UnitQtyinBox,@ItemPicture,@Remarks)";
SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
sqlCmd.Parameters.AddWithValue("@ItemCode", clsitem.ItemCode);
sqlCmd.Parameters.AddWithValue("@ItemName", clsitem.ItemName);
sqlCmd.Parameters.AddWithValue("@ItemDescr", (object)clsitem.ItemDescr ?? DBNull.Value);
sqlCmd.Parameters.AddWithValue("@PurchaseUnit", (object)clsitem.PurchaseUnit ?? DBNull.Value);
sqlCmd.Parameters.AddWithValue("@UnitPrice", (object)clsitem.UnitPrice ?? DBNull.Value);
sqlCmd.Parameters.AddWithValue("@BoxPrice", (object)clsitem.BoxPrice ?? DBNull.Value);
sqlCmd.Parameters.AddWithValue("@UnitQtyinBox", (object)clsitem.UnitQtyinBox ?? DBNull.Value);
sqlCmd.Parameters.AddWithValue("@ItemPicture", (object)clsitem.ItemPicture ?? DBNull.Value);
sqlCmd.Parameters.AddWithValue("@Remarks", (object)clsitem.Remarks ?? DBNull.Value);
clsitem.ItemSysSeq = (int)sqlCmd.ExecuteScalar();
foreach (var i in clsitem.ItemInStock)
{
string query1 = "INSERT INTO [dbo].[FoodItemInStock] (ItemSysSeq,StockSysSeq,OpeningBalance,QtyInHand) ";
query1 = query1 + "Values(@ItemSysSeq,@StockSysSeq,@OpeningBalance,@QtyInHand)";
SqlCommand sqlCmd1 = new SqlCommand(query1, sqlCon);
sqlCmd1.Parameters.AddWithValue("@ItemSysSeq", clsitem.ItemSysSeq);
sqlCmd1.Parameters.AddWithValue("@StockSysSeq", 1);
sqlCmd1.Parameters.AddWithValue("@OpeningBalance", 200);
sqlCmd1.Parameters.AddWithValue("@QtyInHand", 55);
sqlCmd1.ExecuteNonQuery();
}
status = true;
}
}
else
{
status = false;
}
return new JsonResult { Data = new { status = status } };
}
答案 0 :(得分:0)
https://dotnetfiddle.net/EukRIN
这是您的观点
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index5</title>
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script>
$(function () {
$("#theButton").click(function () {
var stockValues = $(".stockvalue");
//https://stackoverflow.com/questions/5489461/pass-array-to-mvc-action-via-ajax
var theArray = [];
$.each(stockValues, function (index, aValue) {
theArray.push(aValue.value);
})
$.ajax({
type: "POST",
url: '/Home/MyAjaxCallMethodPassingArray/',
traditional: true,
data: { 'arrayOfValues': theArray }
});
})
})
</script>
</head>
<body>
<div>
<input type="text" class="stockvalue" />
<input type="text" class="stockvalue" />
<input type="text" class="stockvalue" />
<input type="text" class="stockvalue" />
<input type="button" value="Go" id="theButton" />
</div>
</body>
</html>
这是你的行动
public string MyAjaxCallMethodPassingArray(String[] arrayOfValues)
{
//put a breakpoint here to see the arrayOfValues
return "done";
}