【问题标题】:Binding a dropdown using MVC + Entity Framework without ViewBag使用没有 ViewBag 的 MVC + Entity Framework 绑定下拉列表
【发布时间】:2017-07-13 17:57:21
【问题描述】:

我是 MVC 的新手,我一直在寻找解决这个问题的好方法,但每次遇到这种有效的解决方案时,我都觉得我错过了一些东西。当我去创建视图时,我选择了由数据上下文创建的模型,它会为我自动填充页面。如果这不是最佳实践,那么我愿意解释如何以不同的方式做到这一点。

现在进入这个问题的主题:构建下拉菜单。我一直在使用 ViewBag,因为它可以工作,但我认为这不是最佳做法。

这直接来自我的 Entities 生成的 InventoryRecord 类:

namespace Inventory.DataContext

public partial class InventoryRecord {

    public Nullable<int> TypeId { get; set; }

}

如上所述,我的视图由 Entities 为我创建的内容填充,并进行了一些修改:

@model Inventory.DataContext.InventoryRecord
 <div class="form-group">
        <label class="control-label col-md-2" for="TypeId">Type</label>
        <div class="col-md-10">
            @Html.DropDownList("TypeId", null, "Select...", htmlAttributes: new { @class = "form-control", required = "required" })
            @Html.ValidationMessageFor(model => model.TypeId, "", new { @class = "text-danger" })
        </div>
    </div>

然后在控制器中我有这个:

private InventoryDataSource db = new InventoryDataSource();
ViewBag.TypeId = new SelectList(db.Types.OrderBy(x => x.TypeDesc), "Id", "TypeDesc", type);

这可行,但我想知道如何在不使用 ViewBag 的情况下做到这一点。我在页面上还有其他几个下拉菜单,我想养成按预期方式使用 MVC 的习惯。

【问题讨论】:

  • ViewModels。视图所需的一切都应放入视图模型中。不建议传入实体模型。

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


【解决方案1】:

正如以往建议的那样,使用 viewmodel,这是给你的示例代码

public class RoleViewModel
    {
        public int Id { get; set; }

        [Required]
        [Display(Name = "Role :")]
        public string RoleCode { get; set; }

        public IEnumerable<RoleViewModel> GetRoles()
        {
            return new List<RoleViewModel>
                            {
                                new RoleViewModel() {Id = 1, RoleCode = "Role 1"},
                                new RoleViewModel() {Id = 2, RoleCode = "Role 2"},
                                new RoleViewModel() {Id = 3, RoleCode = "Role 3"}
                            };            
        }
    }

在视图中:

@Html.DropDownListFor(m => m.RoleCode, new SelectList(new MvcApplication1.Models.RoleViewModel().GetRoles(), "Id", "RoleCode"))

注意:只要根据你的要求进行更改,它肯定会起作用。

谢谢

卡提克

【讨论】:

    【解决方案2】:

    就像史蒂夫说的,不要通过实体模型。始终为您的视图创建视图模型类。

    添加视图模型类:

    public class InventoryRecordModel
    {
       [Required]
       public int TypeId;
       //some other properties
    }
    

    将视图更改为:

    @model InventoryRecordModel
    <div class="form-group">
        <label class="control-label col-md-2" for="TypeId">Type</label>
        <div class="col-md-10">
            @Html.DropDownList("TypeId", null, "Select...", htmlAttributes: new { @class = "form-control", required = "required" })
            @Html.ValidationMessageFor(model => model.TypeId, "", new { @class = "text-danger" })
        </div>
    </div>
    

    保持控制器不变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      相关资源
      最近更新 更多