how to pass another parameter from jquery ajax to mvc controller, along with form.serialize()
-
16-06-2021 - |
Frage
I use a jQuery plugin called jscroller, which uses jquery.ajax to make ajax calls. I need to pass in all parameters from a search form, and deliver them to mvc controller, and I think the best way is to put in 'data' field the following expr':
$('#formId').serialize();
My problems is that I need to pass, along with the form values, a 'page' value which is being changed with each call.
How can I put the page value, along with the 'serialize' expression in 'data' field, or is there really another more efficient way to do it?
Lösung
I think
var formdata = $('#formId').serialize();
data: (formdata ? formdata + "&" : "") + "page=" + pageId
will solve your problem
Andere Tipps
You can create a new FormData and just add the parameter on that variable too
var formData = new FormData();
formData.append('pageId', pageId);
var your_form = $('#formId').serializeArray();
$.each(your_form,function(key,input){
formData.append(input.name,input.value);
});
$.ajax({
url: 'url_here',
data: formData,
contentType: false,
processData: false,
type: 'POST'
});
so at MVC controller you will be able to access like
var pageId = Request.Form.Get("pageId");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow