jquery - Submit JSON to MVC Controller without using Ajax -


i have several <input> fields on page collect user input. page uses jquery tabs. once user finished inputting data advance next jquery tab until @ last tab displays information retrieved via ajax. page never refreshes.

the above works fine, implementing option user can convert results pdf can save or print. originally, jquery trigger pdf conversion was:

$('#btnviewprintsavepdf').click(function (event) {     event.preventdefault;      $(this).parents("form")         .attr("method", "post")         .attr("action", "/controller/converttopdf");         .submit(); }); 

in controller:

[httppost] public actionresult converttopdf() {     object model = null;     viewdatadictionary viewdata = new viewdatadictionary(model);      viewdata.add("myvalue1", request["myvalue1"]);     viewdata.add("myvalue2", request["myvalue2"]);     viewdata.add("myvalue3", request["myvalue3"]);      // string writer render html code of view     stringwriter stringwriter = new stringwriter();      // base url     string currentpageurl = this.controllercontext.httpcontext.request.url.absoluteuri;     string baseurl = currentpageurl.substring(0, currentpageurl.length - "controller/pdftemplate".length);      viewdata.add("baseurl", baseurl);      // render index view in html string     viewengineresult viewresult = viewengines.engines.findview(controllercontext, "pdftemplate", null);     viewcontext viewcontext = new viewcontext(         controllercontext,         viewresult.view,         viewdata,         new tempdatadictionary(),         stringwriter     );     viewresult.view.render(viewcontext, stringwriter);      // view html string     string htmltoconvert = stringwriter.tostring();      // create html pdf converter object default settings     htmltopdfconverter htmltopdfconverter = new htmltopdfconverter();      // set license key     htmltopdfconverter.licensekey = "...hidden...";      // convert html string pdf document in memory buffer     byte[] outpdfbuffer = htmltopdfconverter.converthtml(htmltoconvert, baseurl);      // send pdf file browser     fileresult fileresult = new filecontentresult(outpdfbuffer, "application/pdf");     fileresult.filedownloadname = "mypdf.pdf";      return fileresult; } 

again, above code works fine. pdf conversion of .cshtml view occurs , prompted usual dialog box asking if want open pdf or save it.

note original page never gets refreshed , that's way needs remain.

the problem:

i have json object/string gets created in original page. needs submitted above converttopdf() routine along of form fields (checkbox, text, etc). can't figure out how this.

ajax breaks converttopdf() in dialog box no longer pops asking if want open or save pdf.

my intention include following code in converttopdf():

groupchoiceids groupchoiceids = null; if (request["groupchoiceids"] != null) {     javascriptserializer jss = new javascriptserializer();     groupchoiceids = jss.deserialize<groupchoiceids>(request["groupchoiceids"]); } 

the above code snippet works elsewhere in application using ajax post this:

$.ajax({     type: "post",     url: "/central/converttopdf",     datatype: "json",     data: {         groupchoiceids: ('{ 'key1' : 'value1', 'key2' : 'key2', 'key3' : 'value3','specialkey1' : 'special_|_value' }')             },      ....  }); 

so, how can submit groupchoiceids json string converttopdf() without causing page refresh , not breaking open/save functionality?

edit

i've tried placing json string in hidden <input> field, field doesn't submitted , doesn't show in controller after form submission. input field looks this:

<input type="hidden" value="{ 'key1' : 'value1', 'key2' : 'value2', 'key3' : 'value3', 'specialkey' : 'special_|_value' }" id="groupchoiceids"> 

apparently value= contains invalid characters, i'm not sure ones. there standard way of getting json string valid use in hidden value= attribute?

update unable add hidden input field .cshtml , have available in controller no matter tried. used @html.hidden("groupchoiceids") , worked. i'm confused - why can't manually add hidden input field? controller function doesn't have model specified , assume accept/detect input field submitted view?

i don't mind using @html.hidden (it works!) wanted learn why having way. can tell me difference between:

<input id="groupchoiceids" type="hidden" value="" /> 

and

@html.hidden("groupchoiceids") 

add groupchoiceids hidden control inside second form

<input id="groupchoiceids" type="hidden" value="" name="groupchoiceids"/> 

and update value $('#groupchoiceids').val(jsonstringval) in form submit event before ajax call. in mvc action access using request["groupchoiceids"] , de-serialize jsonstring using json.net library.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -