Hi, I am creating a form in asp.net mvc

@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })

How to set input type=password in above code.
I have tried password for but it makes changes to web design

Dani AI

Generated

Quick answer and the usual causes: EditorFor picks an editor template, so to get an actual password input you can either use the PasswordFor helper (simplest), annotate the model so EditorFor emits a password template, or add a custom EditorTemplate. was using EditorFor and saw layout changes after trying a password helper; pointed to CSS. In practice the change usually comes from either passing html attributes in the wrong shape or from CSS that specifically targets input[type="password"].

Try this simplest fix first:

@Html.PasswordFor(m => m.Password, new { @class = "form-control" })

Note: PasswordFor accepts html attributes directly (not wrapped in an htmlAttributes object like some EditorFor overloads). If you copied the attribute shape from EditorFor you may have lost the form-control class and changed the appearance.

If you prefer EditorFor everywhere, annotate the model so the editor chooses a password input:

using System.ComponentModel.DataAnnotations;

public class LoginViewModel
{
    [DataType(DataType.Password)]
    public string Password { get; set; }
}

Or create a reusable EditorTemplate at /Views/Shared/EditorTemplates/Password.cshtml:

@model string
@Html.PasswordFor(m => m, new { @class = "form-control" })

Alternative (use with caution):

@Html.TextBoxFor(m => m.Password, new { type = "password", @class = "form-control" })

Caveat: TextBoxFor will render the model value into the page source (possible exposure). PasswordFor intentionally does not output a persisted value for security. Troubleshooting checklist: inspect the generated HTML to confirm the class and type, search for custom EditorTemplates (String or Password), and check any CSS rules targeting input[type="password"]. Ignore unhelpful tone (see ) and apply the approach that keeps your CSS consistent and your pages secure.

Recommended Answers

All 2 Replies

You don't know how to do this, yet your signature advertises you as an ASP.Net Developer. Do you see the problem here?

commented: In their profile, 50-99 employees. I guess this company does not work well together. +15

I have tried password for but it makes changes to web design

You can fix this with CSS.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.