asp.net mvc - how to use mvc4 model value in knockout viewmodel js -
i using knockout asp.net mvc here view populated mvc controller
html
<html> <input type="text" data-bind="value:name" /> <script src="/scripts/viewmodel.js"></script> </html>
controller
public actionresult xyz(){ var mymodel = new fiestmodel(); mymodel.name = "james"; return view(mymodel); }
viewmodel.js
function mymode(){ var self = ; self.name = ko.observable('@model.name'); }
after doing of when page render input doesn't have specified value james
.
update
i tell guys whole scenario , in application user click on signup facebook
button , facebook return user xyz
action method , want show username in xyz
view . how can api because @anders said me web api .
thanks in advance .
you shouldnt mix server side mvc , client side mvvm. move model population webapi controller method.
load data using jquery.getjson or other framework ajax can use ko.mapping map server data viewmodels
edit: code in viewmodel.js have moved cshtml file if oyu want use @mode.name, please dont it.
update along lines
[httpget] public fiestmodel xyz() { return new fiestmodel("james"); }
with mapping plugin like
viewmodel = function(data) { ko.mapping.fromjs(data, {}, this); }; $.getjson("api/mycontroller/xyz", null, function(data) { ko.applybindings(new viewmodel(data)); });
Comments
Post a Comment