Search code examples
c#asp.net-mvcasp.net-mvc-viewmodel

Dropdown List from View Model Result


I have a working example of a 'ViewModel' that I am thinking will work for a dropdown list. Currently I am using this to do a Foreach and it works fine for that but I need the same result in a dropdown on my form. I have played around with different ideas that I thought might work. None seem to do the trick. I do not have examples because I just normally read a bunch of posts and start throwing code at it to see what sticks. I am still learning so hopefully someone can either point me in the right direction or show me some code that will populate what I need. I believe I have everything in my ViewModel I need and I would like to stay away from Viewbag. Below is my 'ViewModel'.

This is the line I would like to use to populate the dropdown unless there is a better way.

result.Addresses = db.Addresses.Where(a => a.CompanyId == result.CompanyId);

EDIT - This is the now working dropdown for this 'ViewModel'

@Html.DropDownList("AddressId", new SelectList(Model.Addresses, "AddressId", "LocationName"), "Select Address user belongs to", new { @class = "form-control" })

Here is the whole 'ViewModel'

    public class RegisterUserViewModel
{
    public static RegisterUserViewModel GetCompanyId(string userId, CustomerEntities db)
    {
        var qCust = from ad in db.Addresses

                    join anu in db.Users on ad.AddressId equals anu.AddressId
                    join cus in db.CompanyNames on ad.CompanyId equals cus.CompanyId
                    where (anu.Id == userId)
                    select new RegisterUserViewModel()
                    {
                        UserId = userId,
                        CompanyId = cus.CompanyId,
                        AddressId = ad.AddressId
                    };

        var result = qCust.SingleOrDefault();

        if (result != null)
        {
            result.Addresses = db.Addresses.Where(a => a.CompanyId == result.CompanyId);
        };

        return result;
    }

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

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

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

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

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

    public string UserId { get; set; }
    public int CompanyId { get; set; }
    public virtual IEnumerable<Addresses> Addresses { get; set; }
}

I am looking to have the AddressId as the value and LocationName as the Text.


Solution

  • Try as below.

    @Html.DropDownList("address", new SelectList(Model.Addresses, "AddressId", "LocationName"))
    

    As you have mentioned into the comment that you're getting 2nd item as selected value after using @Html.DropDownList(model => model.AddressId..., so what happen here is it will automatically set selected value from your model.AddressId. Just check for that.

    Now if you want to set default value then make sure that your model.AddressId has value same as your default select value.