Render span tag with title attribute with ASP.NET MVC 3 Helpers
Asked Answered
A

5

23

It's possible to add a HTML title attribute to an input tag like so:

@Html.TextBoxFor(model => model.Name, new { title = "Customer name" })

Is there a similar helper for static text? With @Html.DisplayFor(model => model.Name) I can render the text from a model property. How can I add HTML attributes so that I get a span tag rendered like this:

<span title="Customer name">ABC</span>
Afflictive answered 14/5, 2011 at 11:7 Comment(1)
This question's explicitly tagged "MVC 3", but note that for version 4 (razor 2) and up you can just get away with doing e.g. <span title="@Model.NameTooltip">@Model.Name</span>Marou
S
45

Custom html helper is probably the neatest solution.

public static MvcHtmlString SpanFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
{
    var valueGetter = expression.Compile();
    var value = valueGetter(helper.ViewData.Model);

    var span = new TagBuilder("span");
    span.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    if (value != null)
    {
        span.SetInnerText(value.ToString());
    }

    return MvcHtmlString.Create(span.ToString());
}

=>

@Html.SpanFor(model => model.Name, new { title = "Customer name" })
Supernatural answered 14/5, 2011 at 12:8 Comment(3)
That works great! I only had to catch that value can be null (if (value != null) before span.SetInnerText(value.ToString()); Thank you very much!Afflictive
data binded to this spanfor is not available during httpPost... Any update for thatLockett
I had issues with this for data attributes, you need to wrap htmlAttributes with HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) otherwise it doesn't replace _ with - like other objects.Napkin
W
5
<span title="Customer name">@Model.Name</span>
Warrantor answered 14/5, 2011 at 11:49 Comment(2)
or <span title="Customer name">@Html.DisplayTextFor(m => m.Name)</span> if Name can be nullWarrantor
So, is there no built-in helper which could create this span tag?Afflictive
V
2

If you are using @HTML.DisplayFor(model=>model.CustomerName) It will render as text, It will not shows any tag inside the values.

If you want to bind the "DisplayFor" using span , Use the below tag,

<span id="CustomerName">@Html.DisplayFor(model=>model.CustomerName)</span>
Vinyl answered 24/2, 2017 at 10:30 Comment(1)
This solution allowed me to continue to use the display template for my datetime value that @Html.DisplayFor uses as well as add a title to the span.Impatiens
C
0

Ugly, but works ;-) (required field, without rendering .EditorFor content get lost during submit, even HTML comment around does not work, tested on System.Web.Mvc 5.0.0.0)

<b style="display:none">@Html.EditorFor(model => model.UserName)</b>
<input name="UserName" disabled readonly [email protected](model => model.UserName)>

Original looked like:

<input class="text-box single-line" data-val="true" data-val-required="The User Name field is required." id="UserName" name="UserName" type="text" value="Hate Razor !">
Caplin answered 1/9, 2020 at 4:43 Comment(0)
R
-1

You use the below

<span title="Customer name">@Html.DisplayTextFor(m => m.CustomerName)</span>
Rhaetic answered 14/5, 2011 at 12:21 Comment(1)
This doesn't create a span tag.Afflictive

© 2022 - 2024 — McMap. All rights reserved.