I'm not sure exactly what you're looking for here. Traditionally, an editor template will just pass-through the htmlAttributes. For example:
View
@Html.EditorFor(m => m.FooString, new { htmlAttributes = new { @class = "foo" } })
String.cshtml
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue.ToString(), ViewData["htmlAttributes"])
If you're asking about how to do something like set defaults that can then be overridden or added to by passing htmlAttributes
. Then, you're pretty much on your own there. There's nothing existing in MVC to help you out (at least fully). However, I did write my own HtmlHelper extension to take care of this. I actually wrote a blog post to explain what this does and how to use it. I recommend you check it out, but I'll post the code here for completeness.
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
public static partial class HtmlHelperExtensions
{
public static IDictionary<string, object> MergeHtmlAttributes(this HtmlHelper helper, object htmlAttributesObject, object defaultHtmlAttributesObject)
{
var concatKeys = new string[] { "class" };
var htmlAttributesDict = htmlAttributesObject as IDictionary<string, object>;
var defaultHtmlAttributesDict = defaultHtmlAttributesObject as IDictionary<string, object>;
RouteValueDictionary htmlAttributes = (htmlAttributesDict != null)
? new RouteValueDictionary(htmlAttributesDict)
: HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributesObject);
RouteValueDictionary defaultHtmlAttributes = (defaultHtmlAttributesDict != null)
? new RouteValueDictionary(defaultHtmlAttributesDict)
: HtmlHelper.AnonymousObjectToHtmlAttributes(defaultHtmlAttributesObject);
foreach (var item in htmlAttributes)
{
if (concatKeys.Contains(item.Key))
{
defaultHtmlAttributes[item.Key] = (defaultHtmlAttributes[item.Key] != null)
? string.Format("{0} {1}", defaultHtmlAttributes[item.Key], item.Value)
: item.Value;
}
else
{
defaultHtmlAttributes[item.Key] = item.Value;
}
}
return defaultHtmlAttributes;
}
}
Then, in your editor template (Date.cshtml
in this example):
@{
var defaultHtmlAttributesObject = new { type = "date", @class = "form-control" };
var htmlAttributesObject = ViewData["htmlAttributes"] ?? new { };
var htmlAttributes = Html.MergeHtmlAttributes(htmlAttributesObject, defaultHtmlAttributesObject);
}
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue.ToString(), htmlAttributes)
UPDATE
I cannot use @Html.TextBox, but need to write manually <input type="text" {htmlAttributes should be here} />
Why? There's not really anything you can't do with the helper version. However, if you insist on going that route, you're going to have a hard time doing it with Razor. Assuming you can actually write it without Razor syntax errors, the code is going to be virtual unreadable. I'd suggest using TagBuilder
and/or StringBuilder
in pure C# to construct a string and then just make sure you return/set a variable of type MvcHtmlString
:
var output = new MvcHtmlString(builder.ToString());
However, if you're going that far, it kind of negates the purpose of using an editor template, unless you're trying to override one of the default editor templates. Regardless, I'd recommend just creating your own HtmlHelper extension and then either using this directly in your view or using it in your editor template.
htmlAttributes
should be passed to helper that knows how to add it to rendered html tag. I don't understand now what are you trying to achive – Signory