简体   繁体   中英

Sending a server-side object via JSON from Javascript for jqGrid

I have a class in my server-side code, say, something like this:

public class foo
{
   public int FieldOne { get; set; }
   public string FieldTwo { get; set; }
   public float FieldThree { get; set; }
}

Now, in my client-side code, there's a form that loads data in inputs that are meant to be used to retrieve information from the database, and then that information has to be loaded to a jqGrid.

I know how to get the server-side function/action the data from the form if it's in the form of a [HttpPost], something like:

[HttpPost]
public ActionResult Index (foo model)
{
   ...
}

But I can't get the grid to call this method. I have set the parameter "url" of jqGrid to "Controller/Index", but it doesn't call it.

Is there anyway to call it that way? Or is there any other way I can send the form information in object format through Javascript?

Thanks.

UPD:

This is the grid code:

grid.jqGrid({
        url: "Controller/Action",
        datatype: 'json',
        emptyrecords: "No hay proyectos cargados",
        colNames: ['Code', 'Desc', 'Rev', 'Client'],
        colModel: [
                 { name: 'Code', index: 'Code', width: 100 },
                 { name: 'Desc', index: 'Desc asc, Desc', width: 200 },
                 { name: 'Rev', index: 'Rev', width: 100, align: "right" },
                 { name: 'Client', index: 'Client', width: 200, align: "right" }
               ],
        rowNum: 10,
        loadonce: false,
        sortname: 'Code',
        viewrecords: true,
        sortorder: "desc",
        height: 'auto',
        caption: ""
    });

It's the same if I change the URL for the one generated by @Url.Action().

Add this line in your Grid.

mtype: 'GET'

Right now your grid is trying a GET (default) request for a POST only action (due to the attribute you applied). Alternatively, you can remove the POST restriction and add the AllowGet option to your return Json(*******) .

There might be more then one problem but let's start with the easy stuff :)

-Edit-

Also, as mentioned in comments, Fiddlers + Firebugs/Chrome console helps a LOT when working with javascript / ajax / json since they do not output anything visual when there are errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM