Cybercop
Cybercop

Reputation: 8684

MVC4 : DropDownList works but not DropDownListFor

I wanted to have registration form registration form for the admin side.

Right now my DropDownList works but I don't know how to get the value and write it to database(Sql server compact). Then again I thought may be using DropDownList would be better idea and could work but I get error. Here are my model

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

    }

I need to get the value from dropdownlist and set it to Accessright

my controller

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

my view

 <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"/>

when I try to change the @Html.DropDownList("right") to @Html.DropDownListFor((u=>u.AccessRight, new SelectList("right", "value", "text"),"--Select Right--") I get error. Edit: error says "HttpException was unhandled by user code Data Binding: 'System.Char' does not contain property with the name 'value'

Thanks in advance.

Upvotes: 0

Views: 1804

Answers (2)

alok_dida
alok_dida

Reputation: 1733

Try to change below code

from

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

to

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

On submit you will automatically get value in model for AccessRight.

Upvotes: 0

RL89
RL89

Reputation: 1916

Try this

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

In model, Access right should contain a valid id that exist in Viewbag.right that is of Enumerable type.

Access right should contain a value that you want to show as selected in the Dropdown list

EDIT:-

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

Name of the Datavalue field should be same to the list

Hope this helps

Upvotes: 1

Related Questions