Reputation: 355
I always get null arrays when I send them by AJAX to controller which looks like that:
[HttpPost]
public decimal CountAdditionalSellPrice(PaintballItemQuantity[] piq_tab, EventOtherOption[] eoo_tab, int VAT = 8)
{
...
}
I use this script on my HTML page:
<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>
And e.g. in primary2
I have:
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"}
Example additional2
objects in array:
0: {ID: "9", EventID: "676", Name: "ExampleName", Price: "1,00", Quantity: "1"}
My 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; }
}
My controller parameters are always null, but what's strange I'm using same script (only part with getting VAT value is changed) on another view and it works perfect (having same primary2 values as above). Can someone tell me what is wrong here or why I'm having null values recieved from one view but on the other I have my controller parameters filled with values like above?
Upvotes: 0
Views: 672
Reputation: 95
Please provide us the source code of ...
Update 1 - Approach 1
First of all you are declaring the variable vat two times in your script. Only assign the value to vat in the true part of your if statement. In the working script you are getting the vat value from the ViewBag. Let me guess that the return type is a string. In the not working script you assign an integer value to the variable vat. Json data needs to be stringified!
Try to replace this code
var vat = 8;
if ($('#Event_PaymentType').val() == 1)
{
var vat = $('input[type=radio]:checked', '#vatButtons').val();
}
with this
var vat = "8";
if ($('#Event_PaymentType').val() === "1")
{
vat = $('input[type=radio]:checked', '#vatButtons').val();
}
Upvotes: 1
Reputation: 59
put your three parameters in a class and replace parameters with one parameter
Upvotes: 0