Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout Posting nested (master detail) form without ajax (Normal form post)

i have just gone through knockoutjs and watched the video provided on the site. its quite interesting how we can cleanly arrange our js code using knockout. I have also gone through examples where it produces nested or master detail forms but these forms are always submitted through ajax. Is there a way i can post the nested form without ajax without breaking asp.net mvc's default modelbinding. More specifically i want the behavior as in this code demo that i created a while ago. but now i want to do it using knockoutjs and without ajax

like image 296
Muhammad Adeel Zahid Avatar asked Nov 19 '25 23:11

Muhammad Adeel Zahid


1 Answers

Take a look at the answer to this question: Submit json to MVC3 action

It describes using the utility function ko.utils.postJson to submit part or all of your view model using a normal form post. It does require an attribute on the .NET side to properly deserialize it from a form post (as opposed to using AJAX with the content type set to JSON).

Also, check out this thread for some ideas if you need to mix Knockout data with normal form fields: https://groups.google.com/d/topic/knockoutjs/eCQo4yzCSAc/discussion and this thread also describes registering a model binder for certain types, so you don't need to use an attribute: https://groups.google.com/d/topic/knockoutjs/3FEpocpApA4/discussion

So, your best bet is to look at calling ko.utils.postJson(url, ko.toJSON(viewModel))

like image 66
RP Niemeyer Avatar answered Nov 21 '25 17:11

RP Niemeyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!