【问题标题】:MVC 4 Simple Populate DropDown from database modelMVC 4 Simple Populate DropDown 从数据库模型
【发布时间】:2013-01-13 14:16:56
【问题描述】:

我觉得有点傻。

我正在尝试掌握 MVC 4,使用拳击作为功能示例。

我在数据库中有WeightCategoriesHeavyweights 等)和Boxers

看起来很简单。关系是拳击手有一个当前的体重类别,但是当我编辑时,我希望它能够通过下拉菜单进行更改。

如果它是我在代码中自己创建的列表,我知道该怎么做,但是我无法理解如何从WeightCategory 表中“加载”列表并将其显示在视图/模型中拳击手。

所以,这是我的 WeightCategory 项目代码:

[Table("WeightCategories")]
public class WeightCategory
{
    [Key]
    public int WeightCategoryId { get; set; }

    public WEIGHT_CATEGORIES WeightCategoryType { get; set; }

    [Display(Name = "Weight Category Name")]
    [Required]
    [MinLength(5)]
    public string Name { get; set; }
    [Display(Name = "Weight Limit In Pounds")]        
    public int? WeightLimit { get; set; }
}

这是拳击手物品的代码

[Table("Boxers")]
public class Boxer
{
    [Key]
    public int BoxerId { get; set; }

    public WeightCategory CurrentWeightCategory { get; set; }

    [Required]
    public string Name { get; set; }
    public int Wins { get; set; }
    public int Losses { get; set; }
    public int Draws { get; set; }
    public int Kayos { get; set; }
}

在视图中,我真的不知道如何解决这个问题,我很确定这不是自动的,我可能需要在控制器中的某个地方加载表格......我正在寻找最佳实践或其他东西.

在最后的视图中类似的东西:

@Html.DropDownListFor(model => model.CurrentWeightCategory.WeightCategoryId,
                      new SelectList(Model.WeightCategories, "WeightCategoryId", "Name", 
                                     Model.WeightCategories.First().WeightCategoryId))

【问题讨论】:

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


    【解决方案1】:

    你可以设计一个视图模型:

    public class MyViewModel
    {
        public Boxer Boxer { get; set; }
        public IEnumerable<SelectListItem> WeightCategories { get; set; }
    }
    

    然后让您的控制器操作填充并将此视图模型传递给视图:

    public ActionResult Edit(int id)
    {
        var model = new MyViewModel();
        using (var db = new SomeDataContext())
        {
            // Get the boxer you would like to edit from the database
            model.Boxer = db.Boxers.Single(x => x.BoxerId == id);
    
            // Here you are selecting all the available weight categroies
            // from the database and projecting them to the IEnumerable<SelectListItem>
            model.WeightCategories = db.WeightCategories.ToList().Select(x => new SelectListItem
            {
                Value = x.WeightCategoryId.ToString(),
                Text = x.Name
            })
        }
        return View(model);
    }
    

    现在你的视图变成了视图模型的强类型:

    @model MyViewModel
    @Html.DropDownListFor(
        x => model.Boxer.CurrentWeightCategory.WeightCategoryId,
        Model.WeightCategories
    )
    

    【讨论】:

    • 谢谢!通过您的回答,您也帮助我了解了 MVVM 与 MVC。
    • 我用 viewModel 替换了我的 @model 变量,但它因运行时异常而失败:@Html.DropDownListFor( Line 27: model =&gt; model.Boxer.CurrentWeightCategory.WeightCategoryId, Line 28: Model.Boxer.WeightCategories) 运行时异常:'System.Web.Mvc.HtmlHelper' 不包含'DropDownListFor' 的定义和最佳扩展方法重载...有一些无效参数
    • 抱歉,我的发帖有问题,刚刚添加!看来第二个说法不好。现在它指向我在 Boxer DO 中的 WeightCategories 列表,可以吗?
    • 你的第二个论点是错误的。您指定了Model.Boxer.WeightCategories,但正确的是Model.WeightCategories。再看看我的回答。我已将WeightCategories 定义为视图模型的IEnumerable&lt;SelectListItem&gt; 属性。
    • 是的,我的错!现在我只是有一个“未设置实例...”异常。不知道为什么,可能是 WeightCategories 没有填写。
    猜你喜欢
    • 2013-09-18
    • 2016-03-09
    • 2014-02-15
    • 1970-01-01
    • 2019-08-13
    • 2013-12-26
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多