我用淘汰赛JS来在我看来属性绑定到我的视图模型。淘汰赛-JS使用一种称为数据绑定,你必须追加到要在其中要绑定到查看模型对象控件的自定义属性。
I am using Knockout-JS to bind properties in my view to my view model. Knockout-JS uses a custom attribute called 'data-bind' that you have to append to controls in which you want to be bound to view model objects.
例如:
<input type='text' name='first-name' data-bind='value: firstName'/>请注意数据绑定属性。
Notice the 'data-bind' attribute.
在我看来渲染,我有麻烦渲染有这个属性的文本框。我知道了Html.EditorFor,Html.TextBoxFor和Html.TextBox助手都以匿名的对象,您可以用它来指定自定义属性。这个实现的唯一问题是C#不允许破折号作为变量名,所以这不会编译: @ Html.EditorFor(M => m.FirstName,新的{数据绑定=值:名字});
In my view rendering, I am having trouble rendering a textbox that has this attribute. I am aware the Html.EditorFor, Html.TextBoxFor, and Html.TextBox helpers all take an anonymous object that you can use to specify custom attributes. The only problem with this implementation is C# doesn't allow dashes as variable names, so this won't compile: @Html.EditorFor(m => m.FirstName, new { data-bind = "value: firstName" });
我能想到的唯一事情是这样的(在视图模型):
The only thing I can think of is this (in view-model):
public class DataBindingInput { public string Value { get; set; } public string DataBindingAttributes { get; set } } public class MyViewModel { ... public DataBindingValue firstName { get; set; } .... }和一个名为DataBindingInput.cshtml视图模板:
And a view template called "DataBindingInput.cshtml":
@model DataBindingInput <input type='text' data-binding='@Model.DataBindingAttributes' value='@Model.Value'>这个唯一的麻烦是我失去的输入名称的自动生成,这样就不会在后回工作,因为该模型粘合剂具有不知道如何绑定。
The only trouble with this is I lose the automatic generation of the input name so it won't work on a post-back because the model binder has no idea how to bind it.
我怎样才能使这项工作?
How can I make this work?
推荐答案由于上述新月鱼,貌似你可以使用下划线和MVC 3将它们转换为破折号,因为下划线在允许使用HTML属性名。
Thanks to Crescent Fish above, looks like you can just use underscores and MVC 3 will convert them to dashes since underscores aren't allowed in HTML attribute names.
更多推荐
使用EditorFor / TextBoxFor /文本框佣工名以破折号自定义属性
发布评论