当我通过AJAX将它们发送到控制器时,我总是得到空数组:
[HttpPost]
public decimal CountAdditionalSellPrice(PaintballItemQuantity[] piq_tab, EventOtherOption[] eoo_tab, int VAT = 8)
{
...
}
我在HTML页面上使用此脚本:
<script type="text/javascript">
function countAdditionalSell()
{
var primary = [];
var primary2 = [];
var primary_el = {};
$("#primaryAdditionalSell").find($('.form-group').find('input')).each(function () {
primary.push(primary_el[this.name] = this.value);
});
for (var i = 0; i < primary.length; i = i + 3) {
var x = {
ID: primary[i],
Name: primary[i + 1],
Quantity: primary[i + 2]
};
primary2.push(x);
}
var additional = [];
var additional2 = [];
var additional_el = {};
$("#otherAdditionalSell").find($('.form-group').find('input')).each(function () {
additional.push(additional_el[this.name] = this.value);
});
for (var i = 0; i < additional.length - 1; i = i + 6) {
var x = {
ID: additional[i],
EventID: additional[i + 1],
Name: additional[i + 2],
Price: additional[i + 3],
Quantity: additional[i + 4]
};
additional2.push(x);
}
console.log(primary2);
console.log(additional2);
var vat = 8;
if ($('#Event_PaymentType').val() == 1)
{
var vat = $('input[type=radio]:checked', '#vatButtons').val();
}
$.ajax({
type: "POST",
dataType: "json",
async: false,
url: "/EventNotes/CountAdditionalSellPrice",
data: { piq_tab: primary2, eoo_tab: additional2, VAT: vat },
success: function (data)
{
$("#Note_AdditionalSellsCost").val(data);
}
});
countWorkerMoney();
};
</script>
例如在primary2
我有:
0:{ID: "8", Name: "7 Up 1L", Quantity: "0"}
1:{ID: "21", Name: "7 Up 2L", Quantity: "0"}
2:{ID: "25", Name: "CocaCola 1L", Quantity: "0"}
3:{ID: "22", Name: "CocaCola 2L", Quantity: "0"}
数组中的示例additional2
对象:
0: {ID: "9", EventID: "676", Name: "ExampleName", Price: "1,00", Quantity: "1"}
我的ViewModels:
public class PaintballItemQuantity
{
public int ID { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
}
public class EventOtherOption
{
public int ID { get; set; }
public Nullable<int> ItemID { get; set; }
[Display(Name = "Nazwa")]
[Required(ErrorMessage = "Nazwa jest wymagana", AllowEmptyStrings = false)]
public string Name { get; set; }
[Display(Name = "Cena")]
[Range(0.01, Double.MaxValue, ErrorMessage = "Cena nie może być mniejsza niż 0.01!")]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
[Display(Name = "Ilość")]
[DefaultValue(1)]
[Range(0, int.MaxValue, ErrorMessage = "Ilość nie może być mniejsza niż 0!")]
public int Quantity { get; set; }
public int EventID { get; set; }
public virtual Event Event { get; set; }
}
我的控制器参数总是为空,但奇怪的是我在另一个视图上使用相同的脚本(只有部分获取增值税值)并且它工作正常(具有与上面相同的primary2值)。有人可以告诉我这里有什么问题,或者为什么我从一个视图收到空值,但另一方面我的控制器参数填充了上面的值?
答案 0 :(得分:1)
请向我们提供......的源代码。
更新1 - 方法1
首先,您在脚本中声明变量vat两次。仅在if语句的真实部分中将值分配给vat。在工作脚本中,您将从ViewBag获取增值税值。我猜测返回类型是一个字符串。在不工作的脚本中,您为变量vat分配一个整数值。需要对Json数据进行字符串化处理!
尝试替换此代码
var vat = 8;
if ($('#Event_PaymentType').val() == 1)
{
var vat = $('input[type=radio]:checked', '#vatButtons').val();
}
用这个
var vat = "8";
if ($('#Event_PaymentType').val() === "1")
{
vat = $('input[type=radio]:checked', '#vatButtons').val();
}
答案 1 :(得分:0)
将三个参数放在一个类中,并用一个参数替换参数