Post JSON array to mvc controller
Posted
by
Yustme
on Stack Overflow
See other posts from Stack Overflow
or by Yustme
Published on 2012-12-08T15:47:44Z
Indexed on
2012/12/08
17:03 UTC
Read the original article
Hit count: 185
I'm trying to post a JSON array to a 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 java code:
$(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?
© Stack Overflow or respective owner