Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout.js and MVC

Tags:

Just started playing with knockout.Js which is a fantastic framework Steve's really done well with that one. One thing I can't seem to do at the minute is impliment it with my Html Helpers. So for exmaple I've got :

 <%: Html.TextBoxFor(model => model.Division) %> 

but I'd line to use the databind on that but at the minute I cant get the "data-bind" attribute into the helper. I've used attributes before such as @class, Id etc but this one is tricky due to the - any Ideas.. Ive tried:

<%: Html.TextBoxFor(model => model.SupplierName, new { data-bind = "SupplierName"}) %> 

and

 <%: Html.TextBoxFor(model => model.SupplierName, new { "data-bind"" = "SupplierName"}) %> 

but no joy. we heavily use the Editor and Text box helpers and I'd really like to integrate these into the Item's with knock out..

Any help much appretiated

like image 704
Andy Allison Avatar asked Nov 09 '10 10:11

Andy Allison


2 Answers

This should work:

<%: Html.TextBoxFor(model => model.SupplierName, new { data_bind = "SupplierName"}) %> 

Variable names cannot contain a hyphen (-) but if you use an underscore (_) in an HTML attribute, it is automatically converted to a hyphen when its 'rendered'.

like image 126
Tim Avatar answered Sep 29 '22 03:09

Tim


You can supply attributes either as anonymous object or as a dictionary. In this particular case a dictionary should be used:

<%: Html.TextBoxFor(m => m.SupplierName, new Dictionary<string, object> { { "data-bind", "SupplierName" } }) %> 
like image 30
Robert Koritnik Avatar answered Sep 29 '22 03:09

Robert Koritnik