0

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>
c0demaster
  • 738
  • 6
  • 17
  • 1
    Not sure I understand your question - are you asking if your solution is architecturally sound? or are you actually getting errors in your code? With respect to storing the cities in your viewmodel, why don't you create a City model which would store Cities in the database and then expose the cities as a virtual property on your original model - that way you keep "data" (i.e. cities) in the database where it belongs. – slapthelownote Aug 19 '11 at 07:50
  • yes i'm asking for if it's architecturally correct, there is more than 30 selectlist and more than 400 text values so i'm asking which way is more accurate to store in dbase or viewmodel? – c0demaster Aug 19 '11 at 20:58

2 Answers2

5

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] : "")
tahir
  • 1,016
  • 1
  • 11
  • 21
0

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...

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • i don't think there is problem to populate on view or on viewModel, also that's my opinion and code was working well via this method, – c0demaster Aug 20 '11 at 03:41