0

Trying to learn ASP.NET MVC.

I have created a model for signup form.

I have name, phone etc. Then I have:

[Required(ErrorMessage = "Field is missing")]
[DisplayName("PostalCode")]
public int PostalCode { get; set; }

public string[] City =
{
"New York",
"London",
"LA",
"Unknown"
} 


public string[] Team =
{
 "Yankees", "Chelsea","Kings",           
} 

First of, unsure on how to code this in a model, with { get; and set; } ?

Objective with these is to: if postalcode is between 1000-2000 automatically display New York, Yankees on the signup form. And same for 3000-4000, London, Chelsea.

I also want a dropdown for both strings so you can choose whatever you want, and the display should be updated accordingly.

This should all be stored along with the other inputs in a DB.

Quite unsure on how to do this with MVC, so any suggestions?

  • Start with this https://stackoverflow.com/questions/105372/how-to-enumerate-an-enum – VDWWD Dec 02 '19 at 20:35
  • 1
    You sure it makes a good idea for these values to be enums? They look like they could change over time....doesn't sound like a good use of enums. – mason Dec 02 '19 at 20:38
  • Well, yes, they are not supposed to change. City1 could for example be New York and TeamOne NY Yankees – Johnny Foxville Dec 02 '19 at 21:08
  • Question is _**why**_ are `Team` and `CountyTeam` defined as enums? I agree with @mason, considering the additional info you require for each value (ie: _if postalcode is between 1000-2000 automatically display City1 ..._) plus the requirement to store the data in a db, I don't believe this is a good use of enums. – Shai Cohen Dec 02 '19 at 21:15
  • Ok, I will change it to arrays – Johnny Foxville Dec 02 '19 at 21:21

1 Answers1

0

Mvc only render static pages, if you want to make your dropdowns with dynamic data you'll need to write custom JavaScript.

Html.EnumDropdownListFor()

Will only render all your enums in a html select.

As mason said, it will be hard to maintain a full enumeration of cities. There is autocompletion javascript pluggin who can be used with some ajax that can answer to your dynamic needs. (Select2, jqueryui, ...)

Orkad
  • 630
  • 5
  • 15
  • Ok. I just thought it was best practice to use enums. But if an array simplifies things? I don't mind, but still unsure on how to achieve the goal. – Johnny Foxville Dec 02 '19 at 21:20