-1

I have the data seeded into a list on the index page into a table and this is the model for the album to create it using entity frame work, this is all the Model.cs

public enum genre { Rock=1, Pop=2 }

public class Album
{
    public int AlbumID { get; set; }
    public string AlbumName { get; set; }
    public string AlbumArtist { get; set; }
    public genre AlbumGenre { get; set; }
    public Album()
    {
        GenreList = new List<SelectListItem>();
    }
    public IEnumerable<SelectListItem> GenreList { get; set; } 
    public List<Artist> Artists { get; set; }  
}

The controller has the following code for the dropdown list. I get a redline under model in the code at the end, here return View(model); however when I run it, it doesn't specify this as the answer.

public ActionResult Index()
{
    Album model = new Album();
    IEnumerable<genre> genres = Enum.GetValues(typeof (genre)).Cast<genre>();
    model.GenreList = from action in genres
                      select new SelectListItem
                      {
                          Text = action.ToString(),
                          Value = (action.ToString())
                      };
    return View(model);
}

and my index page is where it shows the error. @Html.DropDownListFor(model => model.AlbumID, model.GenreList) is the line in which I get the error on AlbumId and GenreList

@model IEnumerable<revision.Models.Album>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create WOW", "Create"
</p>
@Html.DropDownListFor(model => model.AlbumID, Model.GenreList)
jackman
  • 1
  • 1

1 Answers1

-1

Enums are supported in Razor views from MVC 5.1. Check if you are using any prior version of mvc. Additionally, MVC 5.1 Razor DisplayFor not working with Enum DisplayName this will also help you.

Community
  • 1
  • 1
Lalit Kale
  • 557
  • 6
  • 13