Passing extra parameters to ASP.NET MVC controller

2019-04-16 03:30发布

问题:

I am trying to use jQuery Grid plugin with ASP.NET MVC to display data. It seems examples provided on the web shows that the controller action signature looks like this:

public ActionResult SomeMethod(string sidx, string sord, int page, int rows) { }

Is there any way I can pass extra parameters from various input fields on the page?

Cheers, D.

ADDED: I have not written any code yet but the client side code will be something like:

jQuery(document).ready(function(){ 
      jQuery("#list").jqGrid({
        url:'/Entry/GridData/',
        datatype: 'json',
        mtype: 'POST',
        colNames:['Entry Date','Registration','Registered Name'],
        colModel :[
          {name:'EntryDate', index:'EntryDate', width:40 },
          {name:'Registration', index:'Registration', width:200 },
          {name:'RegisteredName', index:'RegisteredName', width:200 }],
        pager: jQuery('#pager'),
        rowNum:20,
        rowList:[5,10,20,50],
        altRows: true,
        sortable: false,
        viewrecords: true,
        caption: 'My first grid'
      }); 
    });

And the server side code something like:

public ActionResult GridData(string sidx, string sord, int page, int rows)
{
    //Some stuff to get data, which needs a couple of extra parameters on top of the ones in the signature
    return Json(dataCollection);
}

In the GridData action I need a couple more parameters to get the appropriate data. How do I specify these in the javascript, and how do I get hold of them in the controller action?

回答1:

You could use the postData property:

$('#list').jqGrid({
    url: '@Url.Action("SomeMethod")',
    datatype: 'json',
    mtype: 'POST'
    postData: { 
        someParam1: $('#someInput1').val(),
        someParam2: $('#someInput2').val() 
    }
    ...
});

and in your controller action you could add this parameter:

public ActionResult SomeMethod(string someParam1, string someParam2, string sidx, ...) { }


回答2:

Create another action method that corresponds to the purpose of the particular input and accepts the arguments you want to accept. If you need to get back to this action result at the end of processing the other result, call a RedirectToRoute