狼一匹

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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>
posted on 2012-06-29 17:33  狼一匹  阅读(2292)  评论(0)    收藏  举报