Using TextBoxFor to set HTML attributes that have a namespace prefix
I am converting some existing HTML to work with ASP.NET MVC, and have some
forms containing input fields that have additional attributes (that, for
now, I need to preserve) that contain a namespace prefix:
<input type="text" foo:required="true" foo:message="Please enter a
username" />
I would like to use the TextBoxFor helper method overload that allows
htmlAttributes to be specified using collection initializer syntax:
@Html.TextBoxFor(
model => model.Username,
new { foo:required="true", foo:message="Please enter a username" })
However, this syntax is not valid, due to the colon in foo:required (etc).
Instead, I am having to use this more verbose dictionary initializer syntax:
@Html.TextBoxFor(
model => model.Username,
new Dictionary<string, object>
{
{ "foo:required", "true" },
{ "foo:message", "Please enter a username" }
})
Is it possible to use a variation on the first syntax, somehow escaping
(for want of a better word) the colons?
Or
Is this a scenario where the TextBoxFor helper method does not really
help, and would it be simpler to just keep the existing raw HTML, and add
value="@Model.UserName" to the input element?
No comments:
Post a Comment