Implementing Dropdownlist on Asp.net MVC 3 from viewModel

I’m new to .net and mvc platform, i have so many int fields that stores some dropdownlist values, i’ve created fields int type due to database size so i’m implementing dropdownlist via this method, it works but i don’t know if it’s the correct solution to store text of values on viewmodel here is the sample code:

i’m so sorry for my english, please let me know if you don’t understand anything.

model property.cs

    [Required(ErrorMessage = "Bu alan gereklidir!")]
    [Display(Name = "Şehir"), MaxLength(60)]
    public int City { get; set; }

propertymgmtviewModel

 public Property Property { get; set; }
    public IEnumerable<Property> Properties { get; set; }
    public IEnumerable<SelectListItem> Cities
    {
        get
        {
            return new[]
            {
            new SelectListItem { Text="--Select One---", Value = "", Selected=true},
            new SelectListItem { Text="Chicago", Value = "1"},
            new SelectListItem { Text="New York", Value = "2"},
            new SelectListItem { Text="Zimbabwe", Value = "3"},
            };
        }

view for editing view page:

 @Html.DropDownListFor(model => model.Property.City,
        new SelectList(Model.Cities,"Value","Text"))

displaying values viewpage :

   <td>
   @(item.City != null ?
   Model.Cities.SingleOrDefault(c => c.Value == item.City.ToString()).Text
   : "")

        </td>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If I was you, I should use a dictionary key / displayName like this for the SelectList :

one static member (as it is static…) :

static Dictionary<int, string> CitiesDict = new Dictionary<int, string>()
    {
        { -1 , "--Select One---"},
        { 0 ,"Chicago"},
        { 1 ,"New York"},
        { 2 ,"Zimbabwe"},
    };

One Property for the dropdown :

public SelectList Cities { get; set; }

Initialized in constructor like this :

Cities = new SelectList(CitiesDict , "Key", "Value", -1);

Then in the view for editing :

@Html.DropDownListFor(model => model.Property.City, Model.Cities)

And for displaying:

@(CitiesDict.ContainsKey(Model.City) ? CitiesDict[Model.City] : "")

Method 2

I haven’t used Razor syntax yet (still on MVC2), but I believe this part is wrong:

@Html.DropDownListFor(model => model.Property.City,
        new SelectList(Model.Cities,"Value","Text"))

You need to tell the view engine what values to populate the select list with (the value attribute and text associated with the option element), but I don’t know the correct MVC3 syntax to help you fix it. Hopefully someone else can…


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x