Guilherme Oderdenge
Guilherme Oderdenge

Reputation: 5001

PlaceHolder + @Html.TextBox issue

The goal

Apply, with success, the placeholder attribute for @Html.Textbox method.

The problem

There is the following syntax on my application;

@Html.TextBox("term", new { placeholder = "What are you searching for?" })

But, when the TextBox is rendered, the value attribute of the input is placeholder = "What are you searching for?". In other words, the placeholder attribute isn't applied as an attribute, but as an input's value.

Knowledge

I already searched about this question on Google and Stack Overflow, but until now, without success.

This link has a solution with the same syntax that I'm using, but when I pass the second parameter to TextBox(), it is rendered as a value and nothing happens with the third parameter (in our case, new { placeholder = "something" }).

Upvotes: 8

Views: 17787

Answers (3)

Mizanur Rahman
Mizanur Rahman

Reputation: 272

try this for an empty @Html.TextBox

@Html.TextBox("CustomarName" ,null, new { @class = "form-control" , @placeholder = "Search With Customar Name" })

Upvotes: 3

Tim S.
Tim S.

Reputation: 56536

You're calling the string name, object value overload of that method, so the second parameter is being taken as the value, not as htmlAttributes. You should use a different overload of the method, probably string name, object value, object htmlAttributes by specifying an empty value:

@Html.TextBox("term", "", new { placeholder = "What are you searching for?" })

Upvotes: 30

Jason Evans
Jason Evans

Reputation: 29186

There is a third param you need:

@Html.TextBox("term", Model.SomeProperty, new { placeholder = "What are you searching for?" })

The third param are any attributes you wish to include in the HTML output of the input field.

Upvotes: 1

Related Questions