使用Entity Framework 6和ViewModel从数据库表填充ASP.NET MVC中的DropDownList

时间:2017-03-02 07:53:12

标签: asp.net-mvc entity-framework razor viewmodel dropdownlistfor

对于我可以使用ajax / jquery和存储过程快速完成的问题,我一直在摸不着头脑。我想

1)使用Entity Framework和视图模型从数据库表中获取的值填充下拉列表。我不想使用VIEWBAG或VIEWDATA。任何帮助表示赞赏。

2)如何使用包含所有默认字段的View Model生成Create View?脚手架适用于模型,但不适用于视图模型?

我的模特

Date cDate = new SimpleDateFormat("hh:mm aa EEE - MMM, dd yyyy", Locale.ENGLISH).parse(time);

查看模型

Date cDate = new SimpleDateFormat("hh:mm aa  EEE - MMM, dd yyyy", Locale.ENGLISH).parse(time);

我的上下文类别

public class Employee
{
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }
    public string Level { get; set; }
    public int DepartmentId { get; set; }
}

public class Grade
{
    public int ID { get; set; }
    public string Level { get; set; }
}

MY CONTROLLER

public class GradeSelectListViewModel
{

    public Employee Employee { get; set; }
    public IEnumerable<SelectListItem> Grades { get; set; }
    public GradeSelectListViewModel(Employee employee, IEnumerable grades)
    {
        Employee = employee;
        Grades = new SelectList(grades, "Grade", "Name", employee.Level);
    }
}

MY RAZOR PAGE CSHTML

public class EmployeeContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Grade> Grades { get; set; }
}

2 个答案:

答案 0 :(得分:7)

主要问题是,在视图中,您有new SelectList(Model.Grades, "ID", "Level")GradesIEnumerable<SelectListItem>SelectListItem不包含名为ID和{{1}的属性}}

但是,您的代码还存在其他一些问题。首先,视图模型不应包含数据模型,而应该是视图模型

Level

并根据需要添加显示和验证属性。请注意,我删除了构造函数(您似乎没有使用它,但是如果您这样做了,那么您还需要包含一个无参数构造函数,否则在提交POST方法时会抛出异常。我还假设由于您绑定到public class GradeSelectListViewModel { public int? ID { get; set; } // make this ID so you do not need an input for it public string Name { get; set; } .... // other properties of Employee that your editing [Required(ErrorMessage = "..")] public int? Level { get; set; } // make value types nullable to protect against under-posting attacks public IEnumerable<SelectListItem> Grades { get; set; } } 的{​​{1}}属性,Level应该是int的类型。

GET方法中的代码应为

int ID

并在视图中

Grade

另请注意,在POST方法中,如果您返回视图,则需要重新分配Employee employee = db.Employees.Find(id); var model = new GradeSelectListViewModel() { ID = employee.EmployeeID, Name = employee.Name, Level = employee.Level, // convert to int? .... Grades = db.Grades.Select(x => new SelectListItem { Value = x.ID.ToString(), Text = x.Level }) }; return View(model); ,因为 @Html.DropDownListFor(x => Model.Level, Model.Grades, "Select Level") 无效。

答案 1 :(得分:1)

您可以使用以下方法在同一视图中填充三个DropDownListFor:

<强>视图模型:

public ActionResult Create()
{
    //Populate DropDownList binding values
    var model = new GroupViewModel
    {
        //Preselect the Lab with id 2
        //LabId = 2,

        Labs = repository.Labs.Select(c => new SelectListItem
        {
            Value = c.Id.ToString(),
            Text = c.Name
        }),
        Terms = repository.Terms.Select(c => new SelectListItem
        {
            Value = c.Id.ToString(),
            Text = c.Name
        }),
        Schedules = repository.Schedules.Select(c => new SelectListItem
        {
            Value = c.Id.ToString(),
            Text = c.Name
        })
    };
    return View("Create", model);
}



控制器:

@Html.DropDownListFor(m => m.LabId, new SelectList(Model.Labs, "Value", "Text"), 
    "Select", new { @class = "selectpicker" })

@Html.DropDownListFor(m => m.ScheduleId, new SelectList(Model.Schedules, "Value", "Text"), 
    "Select", new { @class = "selectpicker" })

@Html.DropDownListFor(m => m.TermId, new SelectList(Model.Terms, "Value", "Text"), 
    "Select", new { @class = "selectpicker" })



查看:

{{1}}

希望这会有所帮助......