MVC4下拉列表错误

时间:2013-05-27 15:33:16

标签: asp.net-mvc asp.net-mvc-4 html.dropdownlistfor

基于这篇文章 Second answer我尝试为我的注册页面创建一个下拉列表。 注册页面有一个字段,您可以在注册他/她时为用户选择PossibleAccessRight,这应该保存在AccessRight属性中。

现在我甚至无法在下拉列表中显示项目

我的模型看起来像这样

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; }

        public UserModel()
        {
            PossibleRights = new List<string>()
                {
                    {"High"},
                    {"Low"},
                };
        }
    }
控制器中的

我在注册方法中有这个是httppost方法

 [HttpGet]
        public ActionResult Register()
        {

            return View();
        }
[HttpPost]
public ActionResult Register(Models.UserModel user)
        {
            var rights = new UserModel();
            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.Email = user.Email;
                    sysUser.Password = encrpPass;
                    sysUser.PasswordSalt = crypto.Salt;

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

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

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

此方法的视图如下所示

<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.DropDownListFor(u=> u.PossibleRights, new SelectList(Model.PossibleRights))</div>//error at this line(NullReference exception)

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

知道我做错了什么吗?另外,我的方法是显示下拉列表中的项目吗?如果有的话,你能提出更好的建议吗?

1 个答案:

答案 0 :(得分:0)

如果要在视图上显示任何信息,则必须先将此信息提供给视图。现在这段代码:

public ActionResult Register()
{

    return View();
}

根本不提供任何信息。使用默认构造函数创建视图模型,这意味着模型对象为空,因此视图上不显示任何内容(特别是在下拉列表中)。你需要的是一些像这样的初始化:

public ActionResult Register()
{
    UserModel model = new UserModel();
    model.PossibleRights = new List<string>{"Right1", "Right2", "Right3"};
    // or go to db, or whatever

    return View(model);
}

除了dropdown在发布时返回选定的值,在这种情况下是表示权限的字符串。因此,您需要在模型中引入一些字段来存储选择:

public class UserModel
{
    ...
    public List<string> PossibleRights;

    public string SelectedRight;
    ...
}

视图的用法如下:

@Html.DropDownListFor(u => u.SelectedRight, new SelectList(Model.PossibleRights))