ajax - Knockout and MVC POST (JSON and Form posting) -


i don't know if real question or wrong idea on how things how post json object (from knockout) form submit mvc controller?

first, controller:

    [httppost]     public actionresult createloanapp(peopleviewmodel myviewmodel)     {         //do on myviewmodel;         return redirecttoaction("index");     }     [httppost]     public jsonresult createloanapp(string deductions)     {         //do on string deductions;     } 

and view:

<h2>my form </h2> @using (html.beginform(new { @class = "submitform" })) {     <label>loan amount</label>     @html.dropdownlistfor(model => model.loan.loanamount, model.dropdownofloanamount, new { @class = "loanamount", @data_bind = "value: selectedloanamount" })     @html.validationmessagefor(model => model.loan.loanamount)      <label>loan receivable</label>     @html.textboxfor(model => model.loan.loanreceivable, "{0:0,0.00}", new { @class = "loanreceivable", @readonly = true, dir = "rtl", @data_bind = "value: loanreceivable" })     @html.validationmessagefor(model => model.loan.loanreceivable)      <label>interest</label>     @html.textboxfor(model => model.loan.interest, "{0:0,0.00}", new { @readonly = true, @class = "interest", dir = "rtl", @data_bind = "value: interest" })      <table class="input-group">         <tbody data-bind="foreach: loandeductions">             <tr>                 <td><strong data-bind='text: deductionname'></strong></td>                 <td>                     <input class="deductioncode form-control" data-bind='value: amount, valueupdate: "afterkeydown"' /></td>                 <td><a href='#' data-bind='click: $parent.removeline'>delete</a></td>             </tr>         </tbody>     </table>      <button type="button" class="btn btn-danger" data-bind="click: save">save deduction</button>      <button type="submit" class="btn btn-primary">save changes</button> } 

as can see have 2 different save buttons:
1. "save deduction" button calls ajax function posts json string called "deductions" action in controller.
2. "save changes" button on other hand, submit button submits form controller , passes "myviewmodel".

now trying combine 2 buttons 1 , pass 2 objects in single controller.
mean want create action accepts 2 parameters this:

    [httppost]     public actionresult createloanapp(peopleviewmodel myviewmodel, string deductions)     {         // on myviewmodel         //do on string deductions;     } 

is possible , if can show me how done.
appreciated. if need more details comment. thanks!

fortunately, have been in exact same situation. way handled updating data property in beforesend object.

here controller action (mainview form bind , page view model, tabview param 1 knockout model):

[httppost] public actionresult save(mainviewmodel mainview, tabviewmodel tabview) { //do work here } 

and html of view, indicating before send setter (mainscript name of javascript object used manage client side work of page):

@using(ajax.beginform("save",new ajaxoptions {httpmethod="post", onbegin = "mainscript.beforesend", onsuccess="mainscript.onsuccess(data)"}}) { //some html form elements <input type="submit" value="send" id="btnsave" /> }  @section scripts{ //a bunch of scripts loaded here <script type="text/javascript"> $(document).ready(function(){ mainscript.initialize(@html.raw(json.encode(model))); }); </script> } 

the initialize takes in viewmodel can store in client , setup knockout bindings, not important here, nice thing know if haven't learned how yet.

finally, when user hits submit, mainscript.beforesend(), function called:

beforesend: function() {     var tabviewmodel = tabknockoutvm.getmodel();     this.data = this.data + "&" + converttoformdata(tabviewmodel); } 

two things note here:

first, need create or model knockout model matches .net object in parameter. if knockout model identical, can use knockout mapping library. if not, suggest putting function on knockout model builds object you.

second, there needed function converted object format parsed .net. 'converttoformdata' function call comes play, which, (mind you, code found elsewhere):

function converttoformdata(obj,prefix)  {     var dataarray=[];     (var op in obj) {       if(op in obj) {         var k = prefix ?               (isnan(op ) ?                  prefix + "." + op  :                  prefix + "[" + op  + "]") :              op;         var v = obj[op];         dataarray.push(typeof(v==="object"?                converttoformdata(v,k) :                 encodeuricomponent(k)+"=" + encodeuricomponent(v));       }     }     return dataarray.join("&"); } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -