简体   繁体   English

Knockout.js和MVC

[英]Knockout.js and MVC

Just started playing with knockout.Js which is a fantastic framework Steve's really done well with that one. 刚开始玩knockout.Js这是一个很棒的框架史蒂夫真的很好用这个。 One thing I can't seem to do at the minute is impliment it with my Html Helpers. 我现在似乎无法做的一件事就是用我的Html Helpers来表达它。 So for exmaple I've got : 所以对于exmaple我有:

 <%: 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: 我之前使用过@class,Id等属性,但是由于 - 任何想法,这个都很棘手。我试过:

<%: 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 任何有用的帮助

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'. 变量名称不能包含连字符( - )但如果在HTML属性中使用下划线(_),则在“渲染”时它会自动转换为连字符。

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" } }) %>

I used Jim's answer as the base for my MVC 4 solution. 我用Jim的答案作为我的MVC 4解决方案的基础。

Jim's: 吉姆的:

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

Mine: 矿:

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

See Using Html.TextBoxFor with class and custom property (MVC) for how to do it in MVC3. 有关如何在MVC3中执行此操作,请参阅使用Html.TextBoxFor和类以及自定义属性(MVC) Im using MVC2 so the above answer was spot on for me 我正在使用MVC2所以上面的答案对我来说很明显

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM