I’m trying to post a JSON array to an MVC controller. But no matter what I try, everything is 0 or null.
I have this table that contains textboxes. I need from all those textboxes it’s ID and value as an object.
This is my Javascript:
$(document).ready(function () {
$('#submitTest').click(function (e) {
var $form = $('form');
var trans = new Array();
var parameters = {
TransIDs: $("#TransID").val(),
ItemIDs: $("#ItemID").val(),
TypeIDs: $("#TypeID").val(),
};
trans.push(parameters);
if ($form.valid()) {
$.ajax(
{
url: $form.attr('action'),
type: $form.attr('method'),
data: JSON.stringify(parameters),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
$('#result').text(result.redirectTo)
if (result.Success == true) {
return fase;
}
else {
$('#Error').html(result.Html);
}
},
error: function (request) { alert(request.statusText) }
});
}
e.preventDefault();
return false;
});
});
This is my view code:
<table>
<tr>
<th>trans</th>
<th>Item</th>
<th>Type</th>
</tr>
@foreach (var t in Model.Types.ToList())
{
{
<tr>
<td>
<input type="hidden" value="@t.TransID" id="TransID" />
<input type="hidden" value="@t.ItemID" id="ItemID" />
<input type="hidden" value="@t.TypeID" id="TypeID" />
</td>
</tr>
}
}
</table>
This is the controller im trying to receive the data to:
[HttpPost]
public ActionResult Update(CustomTypeModel ctm)
{
return RedirectToAction("Index");
}
What am I doing wrong?
There are lots of issues with your code. Let’s start with the markup. You have a table and inside each row of this table you are including hidden fields. Except that you have hardcoded the
idattribute of those hidden elements meaning that you could potentially end up with multiple elements with the same id in your markup which results in invalid markup.So let’s start by fixing your markup first:
Alright, now you have valid markup. Now let’s move on to the javascript event which will be triggered when some
submitTestbutton is clicked. If this is the submit button of the form I would recommend you subscribing to the.submitevent of the form instead of the.clickevent of its submit button. The reason for this is because a form could be submitted for example if the user presses the Enter key while the focus is inside some input field. In this case your click event won’t be triggered.So:
Alright, next comes the part where you need to harvest the values of the hidden elements which are inside the table and put them into a javascript object that we will subsequently JSON serialize and send as part of the AJAX request to the server.
Let’s go ahead:
So far we’ve filled our parameters, let’s send them to the server now:
Now let’s move on to the server side. As always we start by defining a view model:
and a controller action that will take a collection of this model:
And here’s the final client side code:
Obviously if your view model is different (you haven’t shown it in your question) you might need to adapt the code so that it matches your structure, otherwise the default model binder won’t be able to deserialize the JSON back.