使用枚举选择从数据库填充的 Html.DropDownListFor() 的默认值

发布于 2024-12-07 23:24:34 字数 1463 浏览 0 评论 0原文

当存在以下条件时,我似乎无法在 Html.DropDownListFor 中设置所选项目:

1)所选项目 ID 是从枚举(例如,(int)AnimalType)中提取的,

2)列表是从 例如,在这个未经测试

的伪代码中,View 的 DropDownList 不会选择动物 Dog。请注意,如果我将枚举更改为生成 int 值的静态类,则没有问题。在 Linq select 语句中,如果我尝试将 a.AnimalType 转换为 (int)a.AnimalType,编译器会发出警告。有什么想法吗?

谢谢。

    //--------------In the model
    enum AnimalType
    {
            Dog = 1,
            Cat = 2,
            //etc.
    }

    public class Animal
    {
            public AnimalType AnimalId {get;set;}
            public string Name {get;set;}
            //etc.
    }
    public class AnimalModel
    {
            public AnimalId SelectedAnimal {get;set;}
            public IEnumerable<SelectListItem> AllAnimals {get;set;}
    }

    //--------------In the controller

    AnimalModel model = new AnimalModel();
    model.SelectedAnimal = (AnimalType)1;

    List<Animal> getAllAnimals = Repository.GetAllAnimals();//defined elsewhere

    IEnumerable<SelectListItem> animalList =
                    from a in getAllAnimals
                    select new SelectListItem
                    {
                        Selected = (a.AnimalType == (int)model.SelectedAnimal),
                        Text = a.Name,
                        Value = a.AnimalId.ToString()
                    };
    model.AllAnimals = animalList;

    //--------------In the view

    @Html.DropDownListFor(m => m.Id, Model.AllAnimals)

I can't seem to set the Selected item in an Html.DropDownListFor when the following conditions exist:

1) The selected item ID is drawn from an enum (e.g., (int)AnimalType), and

2) The list is populated from a database (or, actually, any list other than the enum)

For example, in this untested pseudocode, the View's DropDownList would not have the animal Dog selected. Note that if I change the enum to a static class that produces int values, I have no problem. In the Linq select statement, if I try to cast a.AnimalType to (int)a.AnimalType, the compiler complains. Any ideas?

Thanks.

    //--------------In the model
    enum AnimalType
    {
            Dog = 1,
            Cat = 2,
            //etc.
    }

    public class Animal
    {
            public AnimalType AnimalId {get;set;}
            public string Name {get;set;}
            //etc.
    }
    public class AnimalModel
    {
            public AnimalId SelectedAnimal {get;set;}
            public IEnumerable<SelectListItem> AllAnimals {get;set;}
    }

    //--------------In the controller

    AnimalModel model = new AnimalModel();
    model.SelectedAnimal = (AnimalType)1;

    List<Animal> getAllAnimals = Repository.GetAllAnimals();//defined elsewhere

    IEnumerable<SelectListItem> animalList =
                    from a in getAllAnimals
                    select new SelectListItem
                    {
                        Selected = (a.AnimalType == (int)model.SelectedAnimal),
                        Text = a.Name,
                        Value = a.AnimalId.ToString()
                    };
    model.AllAnimals = animalList;

    //--------------In the view

    @Html.DropDownListFor(m => m.Id, Model.AllAnimals)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的奇迹 2024-12-14 23:24:34

尝试使用整数作为选定的动物:

public class AnimalModel
{
    public int SelectedAnimal { get; set; }
    public IEnumerable<SelectListItem> AllAnimals { get; set; }
}

然后在控制器中:

public ActionResult Index()
{
    var animals = Repository.GetAllAnimals();
    var model = new AnimalModel
    {
        // define which item should be selected in the drop down
        SelectedAnimal = (int)AnimalType.Cat,

        // define the list of items in the drop down
        AllAnimals = animals.Select(x => new SelectListItem
        {
            Value = x.AnimalId.ToString(),
            Text = x.Name
        })
    };
    return View(model);
}

最后在视图中:

@model AnimalModel

@Html.DropDownListFor(x => x.SelectedAnimal, Model.AllAnimals)

Try using an integer as the selected animal:

public class AnimalModel
{
    public int SelectedAnimal { get; set; }
    public IEnumerable<SelectListItem> AllAnimals { get; set; }
}

and then in your controller:

public ActionResult Index()
{
    var animals = Repository.GetAllAnimals();
    var model = new AnimalModel
    {
        // define which item should be selected in the drop down
        SelectedAnimal = (int)AnimalType.Cat,

        // define the list of items in the drop down
        AllAnimals = animals.Select(x => new SelectListItem
        {
            Value = x.AnimalId.ToString(),
            Text = x.Name
        })
    };
    return View(model);
}

and finally in the view:

@model AnimalModel

@Html.DropDownListFor(x => x.SelectedAnimal, Model.AllAnimals)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文