Rodrigo Juarez
Rodrigo Juarez

Reputation: 1785

Post jqgrid data to mvc3 controller

How should I post the data entered in a jqgrid plugin to a MVC3 controller?

Thanks In Advance

Some code

Sending data to mvc3 controller

        var lineas = $("#articulosIngresadosTable").getRowData();
        var model = {
            ObraSocialId: $("#idObraSocialTextBox").val(),
            Lineas: lineas
        };

        $.ajax({
            type: 'POST',
            url: '@Url.Action("Nueva", "Factura")',
            data: model,
            success: function (data) { alert(JSON.stringify(data)); },
            dataType: "json"
        });

The controller

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Nueva(FacturaNuevaModel model)
    {
        return Json(model);
    }

The model

public class FacturaNuevaModel
{
    public string ObraSocialId { get; set; }
    public IList<Linea> Lineas { get; set; }

What I can't undestand is that I'm sending the same Json with Poster and works, but not whit jquery from the view

Using the post from the view, ObraSocialId is populated in the controller, and the Lineas collection have the items, but every property in Linea has a null value

Upvotes: 0

Views: 3172

Answers (1)

Rodrigo Juarez
Rodrigo Juarez

Reputation: 1785

The problem was the contentType ...

Here is the working code

var lineas = $("#articulosIngresadosTable").getRowData();
var model = {
    ObraSocialId: $("#idObraSocialTextBox").val(),
    Lineas: lineas
};

var modelString = JSON.stringify(model);

$.ajax({
    type: 'POST',
    url: '@Url.Action("Nueva", "Factura")',
    data: modelString,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) { alert(JSON.stringify(data)); }
});

Upvotes: 1

Related Questions