[ASP.NET MVC] @Html.Password() 、@Html.PasswordFor() 和 <input type="password">
密碼欄位可用 @Html.Password() 快速產生,一個引數時是表示 id 和 name:
@Html.Password("pwd") // 可產生: <input id="pwd" name="pwd" type="password" />
兩個引數時第二個是預設填入的值:
@Html.Password("pwd" , "abc1234") // 可產生: <input id="pwd" name="pwd" type="password" value="abc1234" />
三個引數時可在第三個引數用匿名類別 new { } 帶入 HTML 屬性:
@Html.Password("pwd" , null , new { placeholder = "請輸入密碼", maxLength = "15" }) // 可產生: <input id="pwd" maxLength="15" name="pwd" placeholder="請輸入密碼" type="password" />
我們常常在登入帳號時,也會設計 Model 或 ViewModel:
public class VMLogin { public string Account { get; set; } public string Password { get; set; } }
例如 Controller 帶入一個 VMLogin 到 View:
VMLogin vModel = new VMLogin(); return View(vModel);
在 View 中可用強型別的 PasswordFor() 快速產生:
@model prjMvc.ViewModels.VMLogin @Html.PasswordFor(m => m.Password) // 可產生: <input id="Password" name="Password" type="password" /> // id 與 name 繫結了
也可以在第二個引數帶入 HTML 屬性,加入樣式或事件:
@model prjMvc.ViewModels.VMLogin @Html.PasswordFor(m => m.Password , new { onblur = "checkStrength()" } ) // 可產生: <input id="Password" name="Password" onblur="checkStrength()" type="password" />
- @Html.ActionLink() 和 <a>
- @Html.TextBox() 、@Html.TextBoxFor() 和 <input type="text">
- @Html.TextArea() 、@Html.TextAreaFor() 和 <textarea>
- @Html.Label() 、@Html.LabelFor() 和 <label>
- @Html.Password() 、@Html.PasswordFor() 和 <input type="password">
- @Html.RadioButton() 、@Html.RadioButtonFor() 和 <input type="radio">
- @Html.CheckBox() 、@HtmlCheckBoxFor() 和 <input type="checkbox">
- @Html.DropDownList() 、@Html.DropDownListFor() 和 <select> 與<option>
- @Html.BeginForm() 和 <form>
- @Html.Hidden() 、@Html.HiddenFor() 和 <input type="hidden">
- @Html.DisplayFor() 、@Html.DisplayTextFor()、@Html.DisplayNameFor() 和 @Html.EditorFor()
留言
張貼留言