2

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Puzzle84
  • 540
  • 3
  • 20
  • a bit similar issue http://stackoverflow.com/questions/4579598/c-sharp-mvc-3-using-selectlist-with-selected-value-in-view – Birey Oct 18 '11 at 02:19
  • have you checked the debugger to see if `Genders` is being populated before being sent to the view? – shuniar Oct 18 '11 at 18:13
  • Yes it was getting populated as i actually saw the entries in the page. this was with the first solution. i haven't tried it with the 2nd option but it was basically the same query so i expected it. as for the 3rd option that i just posted it's crashing atm. – Puzzle84 Oct 18 '11 at 18:19
  • @Puzzle84 im confused.. how is it being populated and giving you a null reference? – shuniar Oct 19 '11 at 00:35

3 Answers3

1
 public ActionResult Create()
        {
            ViewData["Genders"] = new SelectList(horseTracker.Genders, "Id", "Name");
            ViewData["LegTypes"] = new SelectList(horseTracker.LegTypes, "Id", "Name");
            ViewData["Characters"] = new SelectList(horseTracker.Characters, "Id", "Name");
            ViewData["Sires"] = new SelectList(horseTracker.Horses.Where(h => h.Gender.Name.Equals("Male") && h.Retired.Equals(true)), "Id", "Name");
            ViewData["Dams"] = new SelectList(horseTracker.Horses.Where(h => h.Gender.Name.Equals("Female") && h.Retired.Equals(true)), "Id", "Name");

            return View();
        }

<div class="editor-label">
            @Html.LabelFor(horse => horse.GenderId)
        </div>
        <div class="editor-field">

            @Html.DropDownList("Id", (SelectList)ViewData["Genders"])

            @Html.ValidationMessageFor(horse => horse.GenderId)   
        </div>

Gets the page running and populated. but on creating the horse. The selected value = null.

Puzzle84
  • 540
  • 3
  • 20
1

Here is a simple example of how to use a dropdown in MVC3.

You should create a view model like this:

public class HorseViewModel
{
    [Required(ErrorMessage = "Please give the horse a name.")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Please select a gender.")]
    public int SelectedGenderId { get; set; }

    public ICollection<Gender> Genders { get; set; }
}

In your controller:

[HttpGet]
public ActionResult Horses()
{
    var model = new HorseViewModel { Horses = db.Genders.ToList() }

    return View(model);
}

And in your model:

@model MvcProject.Models.HorseViewModel

@using (Html.BeginForm()) {
    <div class="editor-label>
        @Html.LabelFor(m => m.Name)
    </div>
    <div class="editor-field>
        @Html.EditorFor(m => m.Name)
        @Html.ValidationMessageFor(m => m.Name)
    </div>
    <div class="editor-label>
        @Html.LabelFor(m => m.SelectedGenderId)
    </div>
    <div class="editor-field>
        @Html.DropDownListFor(m => m.SelectedGenderId,
                              new SelectList(Model.Genders, "Id", "DisplayName"),
                              "-- Select Gender --")
        @Html.ValidationMessageFor(m => m.SelectedGenderId)
    </div>

    <p><input type="submit" value="Submit"/></p>
}

That should give you a form that will post back the selected gender id to the controller.

shuniar
  • 2,592
  • 6
  • 31
  • 38
1

actually,

I fixxed it and the solution was WAY simpler than i expected.

    ViewData["Genders"] = new SelectList(horseTracker.Genders, "Id", "Name");
    ViewData["LegTypes"] = new SelectList(horseTracker.LegTypes, "Id", "Name");
    ViewData["Characters"] = new SelectList(horseTracker.Characters, "Id", "Name");
    ViewData["Sires"] = new SelectList(horseTracker.Horses.Where(h => h.Gender.Name.Equals("Male") && h.Retired.Equals(true)), "Id", "Name");
    ViewData["Dams"] = new SelectList(horseTracker.Horses.Where(h => h.Gender.Name.Equals("Female") && h.Retired.Equals(true)), "Id", "Name");

And this in the cshtml

@Html.DropDownListFor(horse => horse.GenderId, (SelectList)ViewData["Genders"])
Puzzle84
  • 540
  • 3
  • 20