asp.net-mvc-3 – 使用EditorFor/TextBoxFor/TextBox助手的名称中的破折号的自定义属性

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 使用EditorFor/TextBoxFor/TextBox助手的名称中的破折号的自定义属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用Knockout-JS来将我的视图中的属性绑定到我的视图模型。 Knockout-JS使用一个名为“data-bind”的自定义属性,您必须附加到要绑定到其中的控件来查看模型对象。

例:

<input type='text' name='first-name' data-bind='value: firstName'/>

注意’data-bind’属性

在我的视图渲染中,我无法呈现具有此属性的文本框。我知道Html.EditorFor,Html.TextBoxFor和Html.TextBox帮助器都使用一个可以用来指定自定义属性的匿名对象。这个实现的唯一问题是C#不允许破折号作为变量名,所以这不会编译:
@ Html.EditorFor(m => m.FirstName,new {data-bind =“value:firstName”});

我唯一可以想到的是(在视图模型中):

public class DataBindingInput
{
     public string Value { get; set; }
     public string DataBindingAttributes { get; set }
}

public class Myviewmodel
{
    ...
    public DataBindingValue firstName { get; set; }
    ....
}

和一个名为“DataBindingInput.cshtml”的视图模板:

@model DataBindingInput
<input type='text' data-binding='@Model.DataBindingAttributes' value='@Model.Value'>

唯一的麻烦是我失去了自动生成的输入名称,因此它不会在后端工作,因为模型绑定器不知道如何绑定它。

我该怎么做这个工作?

解决方法

感谢上面的Crescent Fish,看起来你可以使用下划线,MVC 3将会将它们转换为破折号,因为HTML属性名称中不允许使用下划线。
原文链接:https://www.f2er.com/aspnet/252394.html

猜你在找的asp.Net相关文章