0

Based on this post Second answerI tried to create a dropdown list for my register page. Register page has a field where you can select the PossibleAccessRight for the user while registering him/her which should be saves in AccessRight Attribute.

Right now i can't even show the items in dropdownlist

My model looks like this

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

in controller i have this in registeration method which is httppost method

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

View for this method looks like this

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

any idea what I'm doing wrong? Also, is my approach to show the items in dropdownlist good? Can you suggest better idea if any?

Community
  • 1
  • 1
Cybercop
  • 8,475
  • 21
  • 75
  • 135
  • Can you show the action method that creates model and passes it into view? – Andrei May 27 '13 at 15:44
  • are you talking about the `Register` action with `[HttpPost]` attribute where i pass `rights` to the view? If not I don't quite get you. – Cybercop May 27 '13 at 15:49
  • I was talking about `Register` action that is triggered when get request is sent. You have already added it in your edit. – Andrei May 27 '13 at 15:58

1 Answers1

0

If you want to display any info on the view, you have to provide this info to the view first. Right now this code:

public ActionResult Register()
{

    return View();
}

does not provide any info at all. Model for the view is created with default constructor, which means that model object is empty, therefore nothing is displayed on the view (particularly in the dropdown list). What you need is some initialization like this:

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

    return View(model);
}

Besides dropdown returns selected value when posted, which is string representing a right in this case. So you need to introduce some field in the model to store the selection:

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

    public string SelectedRight;
    ...
}

Usage on view is the following:

@Html.DropDownListFor(u => u.SelectedRight, new SelectList(Model.PossibleRights))
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • so i need to pass the dropdownlist items from [httpget] method rather than [httpPost] method? – Cybercop May 27 '13 at 20:19
  • 1
    @Biplov13, `HttpGet` is the request you receive when user enters the page (clicks the link, navigates to url). As a response you have to display a form with all the elements in it, including items for drop down. `HttpPost` on the other hand is the request you receive when user enters everything on the form (including choosing drop down option) and submits the form. There you need to know the selection - that is what `SelectedRight` property above for. – Andrei May 28 '13 at 09:30