第一种语法是使用匿名对象,关于如何在 C# 中创建标识符的相同规则适用于该对象 :
- 您可以使用类的任何 unicode 字符
Lu, Ll, Lt, Lm, Lo, or Nl
- 您可以使用“@”转义 C# 关键字,如
new { @class = "foo" };
由于冒号属于Po unicode 类,因此不能在标识符中使用。(在 C# 中,您可以使用静态方法char.GetUnicodeCategory来检查任何字符的类)
此外,并且仅在 MVC 中,当对帮助程序中的 html 属性使用匿名对象时,任何带有下划线的属性名称都将被连字符替换。这是由于方法HtmlHelper.AnonymousObjectToHtmlAttributes
回到您的案例并考虑您的选项,如果这些选项没有被广泛使用(例如在几个视图中),我会考虑使用TextBoxFor
帮助程序的 Dictionary 语法。您仍将获得与模型绑定同步的 id\name 属性的自动生成,并且您将从模型元数据中获得任何其他属性,例如不显眼的验证属性。(尽管查看您想要保留的属性,但似乎您不需要不显眼的验证属性:))
但是,如果 id\name 与属性的名称一样简单,并且您不需要使用帮助程序自动生成的任何其他 html 属性,那么使用原始 HTML 是有意义的。(因为字典语法很丑)
如果您在整个应用程序中广泛使用它,那么在我看来,最明智的方法可能是创建自己的帮助程序,例如@Html.LegacyTextBoxFor(...)
. 该助手将呈现您想要保留的那些遗留属性,并且您可以合并类似于标准助手的附加逻辑来创建 id\name 属性。
像这样的东西:
public class FooAttributes
{
public bool Required { get; set; }
public string Message { get; set; }
}
public static class FooHelper
{
public static MvcHtmlString LegacyTextboxFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, FooAttributes fooAttributes)
{
var fieldName = ExpressionHelper.GetExpressionText(expression);
var fullBindingName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName);
var fieldId = TagBuilder.CreateSanitizedId(fullBindingName);
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var value = metadata.Model;
TagBuilder tag = new TagBuilder("input");
tag.Attributes.Add("name", fullBindingName);
tag.Attributes.Add("id", fieldId);
tag.Attributes.Add("type", "text");
tag.Attributes.Add("value", value == null ? "" : value.ToString());
if (fooAttributes != null)
{
if (fooAttributes.Required) tag.Attributes.Add("foo:required", "true");
if (!String.IsNullOrEmpty(fooAttributes.Message)) tag.Attributes.Add("foo:message", fooAttributes.Message);
}
return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
}
}
这可以用作:
@Html.LegacyTextboxFor(model => model.UserName, new FooAttributes {Required=true, Message="Please enter a value." })
并将生成此 html:
<input foo:message="Please enter a value." foo:required="true" id="UserName" name="UserName" type="text" value="">
一旦你有了自己的助手,你就可以添加额外的逻辑,例如从模型元数据及其数据注释属性生成这些属性的逻辑......
我已经超出预期扩展了我的答案,但我希望它有所帮助!