Problem with PlaceHolder + @ Html.TextBox

goal

Successfully apply the placeholder attribute for the @Html.Textbox method.

Problem

My application has the following syntax:

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

But, when the TextBox displayed, the value input attribute is equal to placeholder = "What are you searching for?" . In other words, the placeholder attribute is not used as an attribute, but as an input value .

Knowledge

I have already searched this question on Google and Stack Overflow, but so far, without success.

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

+4
source share
3 answers

You call string name, object value overload of this method , so the second parameter is accepted as value , and not as htmlAttributes . You should use another method overload, perhaps string name, object value, object htmlAttributes , specifying an empty value :

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

A third parameter is required:

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

The third parameter is any attributes that you want to include input fields in the HTML output.

+1
source

try this for empty @ Html.TextBox

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

Source: https://habr.com/ru/post/1497031/


All Articles