maztt
maztt

Reputation: 12304

asp.net mvc c# tooltip

what is the best and most simplest way to create tooltip text for textboxes

Upvotes: 6

Views: 19473

Answers (4)

Antoine Pelletier
Antoine Pelletier

Reputation: 3326

Just use the title tag :

<input type="text" title="Hello I'm the tool-tip"/>

Mvc way :

@Html.TextBoxFor(t => t.NameOfCustomer, new{ title= "Hello I'm the tool-tip" })

It's not fully customizable as is, but it does not require extra javascript nor a framework.

Upvotes: 3

Russ Cam
Russ Cam

Reputation: 125538

With JavaScript and probably with a framework like jQuery that fits very well with ASP.NET MVC. Using the framework means that someone's alread done the hard work and written a plugin for it!

There is of course the title attribute on text inputs that shows as a popup tip in some browsers.

Upvotes: 4

user3885927
user3885927

Reputation: 3503

I found this to be the simplest and easy to maintain approach:

  1. Create description using data annotation for the property of your model Example:

    [Display(Name="MyTextBox", Description = "Title for your entry")] public string MyTextBox{ get; set; }

  2. Then in your view access the description above using:

    @Html.TextBoxFor(model => model.MyTextBox, new { title = ModelMetadata.FromLambdaExpression(model => model.MyTextBox, ViewData ).Description })

Upvotes: 4

Robert Tanenbaum
Robert Tanenbaum

Reputation: 419

Use the data annotations on your model to put the tooltip in the Description property of the DisplayAttribute.

Then write your own Html Helper function that puts the Description property into the title attribute of the TextBox input field. You can call the helper TextBoxWithTooltipFor

In your view definition you can then replace the call to @(Html.TextBoxFor(...)) with the call to @(Html.TextBoxWithTooltipFor(...))

Here is the code that is tested and works. Picture of code to add tooltip to textbox

Upvotes: 0

Related Questions