使用EditorFor / TextBoxFor /文本框佣工名以破折号自定义属性

编程入门 行业动态 更新时间:2024-10-18 14:18:57
本文介绍了使用EditorFor / TextBoxFor /文本框佣工名以破折号自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用淘汰赛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 /文本框佣工名以破折号自定义属性

本文发布于:2023-08-02 11:37:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:佣工   破折号   自定义   文本框   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!