2

I just want to add a placeholder for my generated input element.

This is what being generated

<input class="text-box single-line" data-val="true" data-val-required="The Username field is required." id="Username" name="Username" type="text" value="">

This is the code that generates HTML output

@Html.EditorFor(model => model.Username)

Of course I can just remove the generated code and write it by myself just including the necessary fields, however maybe it is possible to just pass some kind of parameter to EditorFor that will add placeholder attribute to it?

3 Answers 3

4

You can use @Html.TextBoxFor instead of @HtmlEditorFor and then write htmlAttributes like this:

@Html.TextBoxFor(model => model.Username, new { placeholder = "Username..." })
Sign up to request clarification or add additional context in comments.

Comments

1

Try

@Html.EditorFor(m => model.Username, new { data_val_attribute = "value" });

Most of the helpers will have an overload that accepts an anonymous object as the last parameter for specifying attributes in the generated markup.

1 Comment

@Html.EditorFor(model => model.Username, new { placeholder = "Username..." }) does not work.
0

@Html.EditorFor(model => model.Username, new { placeholder = "Username..." }) should work, unless your model is not empty. Have you checked if your model is empty?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.