0

I'm a little behind on my enums and would appreciate some direction on how to pass an enums from the mvc controller via JsonResult to populate a dropdownlist. I have all my other properties passing just fine. Its only the enum as follows:

Enum

public enum SpeciesType{
  Mammal = 1;
  Reptile = 2; }

ViewModel

public int Id {get; set;}

public string AnimalName {get; set;}

public DateTime DateOfRecording { get; set; }

public SpeciesType Species { get; set; }

Controller

public JsonResult EditAnimalRecord(int animalId){
var model = new EditViewModel();
  var record = (Linq statement in here);

  if(record.Id > 104){

   model.Id = record.Id;
   model.AniamlName = record.Name;
   model.DateOfRecording = record.DateEntered;
   model.Species = ???(Need to pass enums here) to populate dropdown via JS   
   }
return Json(model, JsonRequestBehavior.AllowGet)
}
Alex
  • 443
  • 3
  • 18
  • hi nice link is [here](https://stackoverflow.com/questions/20043445/how-to-pass-enum-from-view-to-model-asp-net-mvc) might it will resolve your situation – delta12 Jun 03 '18 at 16:47
  • By using Enum.GetValues(typeof(SpeciesType)).Cast().ToList() I am getting back the integer values of those two items populating my dropdown element, but not the names. Any suggestions? Using GetNames instead of GetValues throws a "Specified cast is not valid" error. – Alex Jun 03 '18 at 20:21
  • check this link almost similar to your problem [here](https://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc) – delta12 Jun 05 '18 at 18:19

0 Answers0