asp.net mvc - How to extend formdata with Javascript and post it to the Controller -
this question has answer here:
- jquery form.serialize , other parameters 5 answers
how can somehow extend formdata in javascript variable available in controller post action.
so want is, add variable update (which bool) , send formdata controller.
here's code of .js file & controller code
bootbox.confirm("are sure want override quotation?", function (result) { if (result === null) { //do nothing } else if (result) { if (!$form[0].checkvalidity()) { return false; } var btn = $('#savequotationform'); btn.attr('onclick', ''); btn.attr('class', 'glyphicon glyphicon-floppy-save'); btn.text('saving...'); var formurl = $form.attr('action'); var formdata = $form.serialize(); console.log(formurl + " =formurl"); console.log(formdata + " =formdata"); //tricky part, want this: //var update = new boolean(false); //formdata.append(update, true); --> not way go :), got idea's?** $.post(formurl, formdata, function (data) { if (data && data.state === 'success') { btn.attr('class', 'glyphicon glyphicon-floppy-saved'); btn.text('saved'); $('#placeholderalert').append('<div class="alert alert-success" role="alert"><strong>saved</strong> ' + data.message + '</div>'); } else { btn.attr('class', 'glyphicon glyphicon-floppy-remove'); btn.text('saving failed'); } }); } }); [httppost] [validateantiforgerytoken] public jsonnetresult products_quotation_save(vmproducts_quotation_save quotation) { quotation.datecreated = datetime.now; quotationcache.instance.savequotation(getclientcode(httpcontext), getusername(httpcontext), quotation, musecontext); return jsonnet(new { state = "success", message = "quotation '" + quotation.name + "' has been saved reference " + quotation.shortcode + ". <a href=" + url.action(routeitemaction.resaplus_quotations) + " class=\"alert-link\">go overview</a>" }); }
edit: indeed duplicate of question, apologies that. fixed 1 of answers provided here: jquery form.serialize , other parameters
so created variable , added $.param, in controller have access param.
var update = { 'update': true }; var formurl = $form.attr('action'); var formdata = $form.serialize() + '&' + $.param(update); $.post(formurl, formdata, function (data) { }); [httppost] [validateantiforgerytoken] public jsonnetresult products_quotation_save(vmproducts_quotation_save quotation, bool update) { }
you can use serializearray()
create update-able object. http://api.jquery.com/serializearray/
var formdata = $form.serializearray(); formdata.push({name: 'update', value: true}); $.post(formurl, formdata, function (data)
Comments
Post a Comment