c# - ASP.NET MVC - Display value for a coded field -


after few years off asp.net, started come back, mvc. suppose have movie class this:

public class movie {     public int id { get; set; }      [stringlength(60, minimumlength = 3)]     public string title { get; set; }      [display(name = "release date")]     [datatype(datatype.date)]     [displayformat(dataformatstring = "{0:yyyy-mm-dd}", applyformatineditmode = true)]     public datetime releasedate { get; set; }      [required]     public int genre { get; set; }      [range(1, 100)]     [datatype(datatype.currency)]     public decimal price { get; set; }      [regularexpression(@"^[a-z]+[a-za-z''-'\s]*$")]     [stringlength(5)]     public string rating { get; set; } } 

i changed field genre in create/edit view user chooses selectbox according following:

1 = action; 2 = adventure; 3 = comedy; 4 = horror; 5 = disaster; 6 = political; 7 = thriller; 

is there easy way display corresponding "human" value instead of plain integer in view/details page?

(in django know well, need movie.get_genre_display(): get_foo_display() )

if want display enum string, change in model int yourenumtype:

public class movie {     ...     [required]     public genreenum genre { get; set; }     ... } 

and in razor write

@html.displayfor(model=> model.genre ) 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -