Html.Password and Html.PasswordFor in MVC Framework

March 13, 2018 0 Comments A+ a-

ASP .Net

Html.Password and Html.PasswordFor

You learn on this topic for How can you create password field in MVC Framework by using HTMLHelper methods.
HTMLHelper class provide two extension methods for generate text field.
  • Html.Password()
  • Html.PasswordFor()


Html.Password() is loosely typed method. So It always require property name as string.
It doesn't get error at compile time.
It have so many overloaded methods such as
  • Password(String)
  • Password(String, Object)
  • Password(String, Object, IDictionary<String, Object>)
  • Password(String, Object, Object)
Known more details on the Password, Please click it here.


Html.PasswordFor() also used for the password field in view for the model object property specified using a lambda expression.
This method is a strongly typed extension method.
It gets error at compile time, if you assign wrong property name.
It have so many overloaded methods such as
  • PasswordFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>)
  • PasswordFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IDictionary<String, Object>)
  • PasswordFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, Object)
Known more details on the PasswordFor, Please click it here. 

We will discuss this on the below example.
  public class EmployeeModel
        public int ID { get; set; }
        public string Name { get; set; }
        public string AccountPassword { get; set; }
        public string Location { get; set; }

public class EmployeeController : Controller
        public ActionResult Create()
            return View();

@model MVCIntro1.Models.EmployeeModel
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";


@using (Html.BeginForm()) { @Html.Password("AccountPassword", new { @class = "labelcolor form-control"}) @Html.PasswordFor(model => model.AccountPassword, new {@class = "labelcolor form-control" }) }
We have successfully completed our code part. Let's run the application and see the appropriate output
Html.Password and Html.PasswordFor in MVC Framework
Fig -1
The above fig -1 is the output for above program.
HTML Output: