So First i tried to create ViewData["whatever"] object like so
ViewData["Blah"] = new selectList("Do cool stuff");
That resulted in an error saying and int32 is not an IEnummerable (no shit);
Then i tried another approach
List<SelectItem> coolList = db.Select(new selectItem("bunch of cool stuff"));
ViewData["Blah"] = coolLost;
That resulted in a selectedIndex that would never change.
Now for my final attempt.
I've added ICollections to my model. Added the collectionId.
For example
public class Horse
{
public int Id { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required(ErrorMessage = "Gender is required")]
public int? GenderId { get; set; }
public string Color { get; set; }
public int? LegTypeId { get; set; }
public int? CharacterId { get; set; }
public int Hearts { get; set; }
public bool Retired { get; set; }
// Parents
public int? SireId { get; set; }
public int? DamId { get; set; }
// Internals
public int Stamina { get; set; }
public int Speed { get; set; }
public int Sharp { get; set; }
// Special
public int Dirt { get; set; }
// Externals
public int Start { get; set; }
public int Corner { get; set; }
public int OutOfTheBox { get; set; }
public int Competing { get; set; }
public int Tenacious { get; set; }
public int Spurt { get; set; }
//Genders
public virtual ICollection<Gender> Genders { get; set; }
//LegTypes
public virtual ICollection<LegType> LegTypes { get; set; }
//Characters
public virtual ICollection<Character> Characters { get; set; }
//Dams
public virtual ICollection<Horse> Dams { get; set; }
//Sires
public virtual ICollection<Horse> Sires { get; set; }
//Races
public virtual ICollection<Race> RaceResults { get; set; }
//Training
public virtual ICollection<Training> TrainingResults { get; set; }
}
and in my cshtml file.
<div class="editor-label">
@Html.LabelFor(horse => horse.GenderId)
</div>
<div class="editor-field">
@Html.DropDownListFor(horse => horse.GenderId, new SelectList(Model.Genders, "Id", "Name", Model.GenderId), "-- Fill Out --")
@Html.ValidationMessageFor(horse => horse.GenderId)
</div>
And that just gives me an error saying i have a null reference.
I am so out of ideas.
And then there is the even better one as you can see in my model. i have a list of sires and a list of dams.
but they are both of type horse. How do i manage to get all my horses in that list.
Thanks in advance for any advice that will trigger light bulbs in my head.
Also trying this right now:
var genders = horseTracker.Genders.Select(g => new SelectListItem { Text = g.Name, Value = g.Id.ToString() }).ToList();
var legtypes = horseTracker.LegTypes.Select(l => new SelectListItem { Text = l.Name, Value = l.Id.ToString() }).ToList();
var characters = horseTracker.Characters.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }).ToList();
var sires = horseTracker.Horses.Where(h => h.Gender.Equals("Male") && h.Retired.Equals(true)).Select(h => new SelectListItem { Text = h.Name, Value = h.Id.ToString() }).ToList();
var dams = horseTracker.Horses.Where(h => h.Gender.Equals("Female") && h.Retired.Equals(true)).Select(h => new SelectListItem { Text = h.Name, Value = h.Id.ToString() }).ToList();
ViewData["Genders"] = genders;
ViewData["LegTypes"] = legtypes;
ViewData["Characters"] = characters;
ViewData["Sires"] = sires;
ViewData["Dams"] = dams;
This gives me an error saying that LinQ can't use toString() apparently since that's an unsafe method.