knockout.js - How to data-bind with Razor and Knockout -


i'm trying set knockout-bound function's parameter razor. i've tried...

<a href="#" data-bind="click: function() { return myfunc(@type.sometype.tostring()); }">     click me </a> 

and..

<a href="#" data-bind="click: function() { return myfunc(@html.raw(type.sometype.tostring())); }">     click me </a> 

and..

<a href="#" data-bind="click: function() { return myfunc('@html.raw(type.sometype.tostring())'); }">     click me </a> 

and..

<a href="#" data-bind="click: function() { return myfunc('@(html.raw(type.sometype.tostring()))'); }">     click me </a> 

is there right way this?

(background: use in bootstrap dropdown)

you can try surround data-bind attribute single quotes (') , pass parameter using json.encode.

full example:

@{     // string lot of special characters     string mystr = "abc\"\\/'#{}@.:xyz"; }  <span data-bind='click: function() { myfunc(@json.encode(mystr)); }'>     click me </span>  <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-debug.js"></script> <script>     ko.applybindings({         myfunc: function (myparam) {             console.log(myparam);         }     }); </script> 

in case be:

<a href="#" data-bind='click: function() { return myfunc(@json.encode(type.sometype.tostring())); }'>     click me </a> 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -