EF 4.1 + MVC下拉列表问题

时间:2011-10-17 23:50:11

标签: c# asp.net-mvc entity-framework

首先我尝试创建ViewData [“whatever”]对象,如此

ViewData["Blah"] = new selectList("Do cool stuff");

导致错误说明并且int32不是IEnummerable(没有狗屎);

然后我尝试了另一种方法

List<SelectItem> coolList  = db.Select(new selectItem("bunch of cool stuff"));

ViewData["Blah"] = coolLost;

这导致了一个永远不会改变的selectedIndex。

现在是我的最后一次尝试。

我已将ICollections添加到我的模型中。 添加了collectionId。

例如

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; }
}

并在我的cshtml文件中。

   <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>

这只是给我一个错误,说我有一个空引用。

我的想法太过分了。

然后在我的模型中可以看到更好的一个。 我有一份公猪名单和一份水坝名单。

但他们都是马。 我如何设法将所有马匹放在该列表中。

提前感谢任何可以触发灯泡的建议。

现在也尝试这个:

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;

这给了我一个错误,说LinQ显然不能使用toString(),因为那是一个不安全的方法。

3 个答案:

答案 0 :(得分: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>

获取正在运行和填充的页面。 但在创造马。 所选值= null。

答案 1 :(得分:1)

以下是如何在MVC3中使用下拉列表的简单示例。

您应该创建一个这样的视图模型:

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; }
}

在您的控制器中:

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

    return View(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>
}

这应该会为您提供一个表格,将选定的性别ID发回给控制器。

答案 2 :(得分:1)

实际上,

我修复了它,解决方案比我预期的简单。

    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");

这是在cshtml

@Html.DropDownListFor(horse => horse.GenderId, (SelectList)ViewData["Genders"])