0

我想有报名表格管理员方面的注册表单。

现在我的DropDownList作品,但我不知道如何获取值并将其写入数据库(Sql server compact)。然后我再次认为可能会使用DropDownList更好的主意并且可以工作,但我得到了错误。这是我的模型

 public class UserModel
    {
        public int UserId { get; set; }

        [Required]
        [EmailAddress]
        [StringLength(100)]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email ID ")]
        public string Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [StringLength(20, MinimumLength = 6)]
        [Display(Name = "Password ")]
        public string Password { get; set; }

        [Required]
        [Display(Name = "First Name ")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last Name ")]
        public string LastName { get; set; }

        [Required]
        [Display(Name = "Address ")]
        public string Address { get; set; }

        public List<string> PossibleRights;

        [Required]
        [Display(Name = "Access Rights")]
        public string AccessRight { get; set; }

    }

我需要从中获取值dropdownlist并将其设置为Accessright

我的控制器

 public List<SelectListItem> UserAccessRight()
        {
            var rights = new List<SelectListItem>
                {
                    new SelectListItem {Text = "Full", Value = "0"},
                    new SelectListItem {Text = "Partial", Value = "1"}
                };
            return rights;
        }

        [HttpGet]
        public ActionResult Register()
        {
            var rights = UserAccessRight();
            ViewBag.right = rights;
            return View();
        }

        [HttpPost]
        public ActionResult Register(Models.UserModel user)
        {

            if (ModelState.IsValid)
            {
                using (var db = new DBaseEntities())
                {
                    var crypto = new SimpleCrypto.PBKDF2();

                    var encrpPass = crypto.Compute(user.Password);

                    var sysUser = db.SystemUsers.Create();

                    sysUser.FirstName = user.FirstName;
                    sysUser.LastName = user.LastName;
                    sysUser.Address = user.Address;
                    sysUser.Email = user.Email;
                    sysUser.Password = encrpPass;
                    sysUser.PasswordSalt = crypto.Salt;

                    sysUser.AccessRight = user.AccessRight;


                    db.SystemUsers.Add(sysUser);
                    db.SaveChanges();

                    return RedirectToAction("Index", "Home");

                }
            }
            else
            {
                ModelState.AddModelError("","Login data is incorrect.");
            }
            return View();
        }

        private bool IsValid(string email, string password)
        {
            var crypto = new SimpleCrypto.PBKDF2();

            bool isValid = false;
            using (var db = new DBaseEntities())
            {
                var user = db.SystemUsers.FirstOrDefault(u => u.Email == email);

                if (user!= null)
                {
                    if (user.Password == crypto.Compute(password, user.PasswordSalt) )
                    {
                        isValid = true;
                    }
                }

            }

            return isValid;
        }

我的观点

 <h4>Signup Now!</h4>


            <div class="editor-label">@Html.LabelFor(u=> u.FirstName)</div>
            <div class="editor-field"> @Html.TextBoxFor(u=> u.FirstName)</div>   
            <br/>
            <div class="editor-label">@Html.LabelFor(u=> u.LastName)</div>
            <div class="editor-field"> @Html.TextBoxFor(u=> u.LastName)</div> 
            <br/>
            <div class="editor-label">@Html.LabelFor(u=> u.Address)</div>
            <div class="editor-field"> @Html.TextBoxFor(u=> u.Address)</div> 
            <br/>
            <div class="editor-label">@Html.LabelFor(u=> u.Email)</div>
            <div class="editor-field"> @Html.TextBoxFor(u=> u.Email)</div> 
            <br/>
            <div class="editor-label">@Html.LabelFor(u=> u.Password)</div>
            <div class="editor-field"> @Html.PasswordFor(u=> u.Password)</div> 
            <br/>
            <div class="editor-label">@Html.LabelFor(u=> u.AccessRight)</div>
            <div class="editor-field"> @Html.DropDownList("right")</div>

            <br/>
            <input type="submit" value="Register"/>

当我尝试将其更改@Html.DropDownList("right")为时 @Html.DropDownListFor((u=>u.AccessRight, new SelectList("right", "value", "text"),"--Select Right--")出现错误。编辑:错误说“用户代码数据绑定未处理 HttpException:‘System.Char’不包含名为‘value’的属性

提前致谢。

4

2 回答 2

1

尝试这个

 @Html.DropDownListFor(model => model.AccessRight, new SelectList(ViewBag.right , "id", "Text "))

在模型中,访问权限应包含存在于Viewbag.right中的 Enumerable 类型的有效 id。

访问权限应包含您希望在下拉列表中选择的值

编辑:-

 @Html.DropDownListFor(model => model.AccessRight, new SelectList(ViewBag.right , "value", "Text "))

数据值字段的名称应与列表相同

希望这可以帮助

于 2013-05-28T09:09:48.913 回答
0

尝试更改以下代码

<div class="editor-field"> @Html.DropDownList("right")</div>

<div class="editor-field"> @Html.DropDownList("AccessRight","Provide list of items here")</div>

提交时,您将自动获得 AccessRight 模型中的值。

于 2013-05-28T09:29:27.600 回答