MVC3 @Html.EditorFor控件初始值
只要在return View 的时候设置对应实体字段的值 就行了
实体类
View Code
public class LogOnModel { [Required] [Display(Name = "用户名")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] [Display(Name = "密码")] public string Password { get; set; } [Display(Name = "记住用户名")] public bool RememberMe { get; set; } }
后台代码
View Code
public ActionResult LogOn() { HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get(COOKIE_NAME_FOR_USER); return View(new LogOnModel() { UserName = cookie != null ? cookie.Value : "" }); }
前台代码
View Code
<div class="accountDiv"> <div class="accountDivwrap"> <h2> 登录 </h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <p> @Html.LabelFor(model => model.UserName): @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) </p> <p> @Html.LabelFor(model => model.Password): @Html.EditorFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) </p> <p> <label>@Html.EditorFor(model => model.RememberMe)</label> @Html.LabelFor(model => model.RememberMe) </p> <div class="submitDiv"> <input type="submit" value="" /> </div> }</div> </div>

浙公网安备 33010602011771号