使用实体框架(型号的.edmx)及MVC3创建一个下拉列表;剃刀观点与放大器;&安培;插入数据库记录到多个表 [英] Create a Dropdown List for MVC3 using Entity Framework (.edmx Model) & Razor Views && Insert A Database Record to Multiple Tables

查看:171
本文介绍了使用实体框架(型号的.edmx)及MVC3创建一个下拉列表;剃刀观点与放大器;&安培;插入数据库记录到多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读在这里了解如何创建剃刀视图中MVC 3下拉列表物品100的,我无法找到一个适合我的情况。小时左右,并寻找答案小时后,我决定发布这个问题,看是否有人有答案,或者能帮助我。谢谢你在前进!

情况:
我最终想要创建一个视图给员工添加到数据库。

Situation: I am ultimately trying to create a View to Add an Employee to a Database.

下面是我使用的.edmx模式的图像(即由使用的表创建()。)

Here is an image of the .EDMX Model that I am using (the tables that will be used by the create().):

目标:


  1. 创建一个员工(我有Create.cshtml(强类型)与局部视图为StaffNotify复选框制造){我使用一个单独的@model从通知创建视图不知道管窥是安全??? @model ShadowVenue.Models.Employee&安培; @model ShadowVenue.Models.StaffNotify)

  1. Create an Employee (I have the Create.cshtml (strongly typed) made with a Partial View for the StaffNotify Checkboxes) {I am using a separate @model in the Notify Partial View from the Create View not sure if that is safe??? @model ShadowVenue.Models.Employee & @model ShadowVenue.Models.StaffNotify)

创建StaffTypeId一个下拉框(将插入来自表StaffType(其中有一个1对多的关系)的[StaffTypeId]值,但是会显示在下拉列表中选择[类型]字符串值)

Create a Dropdown box for StaffTypeId (that will insert the [StaffTypeId] value from the Table "StaffType" (which has a 1 to many relationship), but will show the [Type] string value in the dropdown)

创建GenderId一个下拉框(将插入来自表性别(其中有一个1对多的关系)的[GenderId]值,但是会显示在下拉列表中[两性]字符串值)

Create a Dropdown box for GenderId (that will insert the [GenderId] value from the Table "Genders" (which has a 1 to many relationship), but will show the [Gender] string value in the dropdown)

将记录到数据库中(我有工作人员通知与STAFFID是主键在1对1的关系一个单独的表)

Insert the Record into the database (I have the Staff Notifications in a separate table with a 1 to 1 relationship on the StaffId Primary Key)

我似乎有与控制器code这个麻烦。

I seem to be having the trouble with the Controller code for this.

我不知道我是否应该创建EDMX模型中的存储过程,或者想出一些查询或方法的语法,不知道哪个是最好的方式。

I am not sure if I should create Stored Procedure within the EDMX model, or come up with some query or method syntax, not sure which is the best way.

使用实体框架模型这是我的第一个大型MVC3应用程序。

This my First Large MVC3 App using Entity Framework Model.

感谢您的时间。我真的AP preciate懂行的用户在这个网站。

Thank you for your time. I really do appreciate the knowledgeable users on this site.

(如果你需要了解任何导航属性名称,以帮助该解决方案只是让我知道,我将它们提供给您)

(if you need to know any of the Navigation Property Names in order to help with the solution just let me know, I will provide them to you)

推荐答案

不要直接通过DB模式,以你的观点。你幸运地被使用MVC,因此使用视图模型封装。

Don't pass db models directly to your views. You're lucky enough to be using MVC, so encapsulate using view models.

创建一个视图模型类是这样的:

Create a view model class like this:

public class EmployeeAddViewModel
{
    public Employee employee { get; set; }
    public Dictionary<int, string> staffTypes { get; set; }
    // really? a 1-to-many for genders
    public Dictionary<int, string> genderTypes { get; set; }

    public EmployeeAddViewModel() { }
    public EmployeeAddViewModel(int id)
    {
        employee = someEntityContext.Employees
            .Where(e => e.ID == id).SingleOrDefault();

        // instantiate your dictionaries

        foreach(var staffType in someEntityContext.StaffTypes)
        {
            staffTypes.Add(staffType.ID, staffType.Type);
        }

        // repeat similar loop for gender types
    }
}

控制器:

[HttpGet]
public ActionResult Add()
{
    return View(new EmployeeAddViewModel());
}

[HttpPost]
public ActionResult Add(EmployeeAddViewModel vm)
{
    if(ModelState.IsValid)
    {
        Employee.Add(vm.Employee);
        return View("Index"); // or wherever you go after successful add
    }

    return View(vm);
}

然后,终于在您的视图(你可以使用Visual Studio先脚手架吧),继承类型更改为ShadowVenue.Models.EmployeeAddViewModel。此外,在下拉列表中去,使用:

Then, finally in your view (which you can use Visual Studio to scaffold it first), change the inherited type to ShadowVenue.Models.EmployeeAddViewModel. Also, where the drop down lists go, use:

@Html.DropDownListFor(model => model.employee.staffTypeID,
    new SelectList(model.staffTypes, "ID", "Type"))

和类似的性别下拉

@Html.DropDownListFor(model => model.employee.genderID,
    new SelectList(model.genderTypes, "ID", "Gender"))

每个注释更新

有关性别,你也可以做,如果你可以没有在上面建议的视图模型(不过,转念一想,也许我会生成视图模型为IEnumerable这个服务器端)的genderTypes。因此,代替新的SelectList ... 下面,你会用你的IEnumerable

For gender, you could also do this if you can be without the genderTypes in the above suggested view model (though, on second thought, maybe I'd generate this server side in the view model as IEnumerable). So, in place of new SelectList... below, you would use your IEnumerable.

@Html.DropDownListFor(model => model.employee.genderID,
    new SelectList(new SelectList()
    {
        new { ID = 1, Gender = "Male" },
        new { ID = 2, Gender = "Female" }
    }, "ID", "Gender"))

最后,另一种选择是一个查找表。基本上,你保持一个查找类型相关的键值对。一类型的一个例子可以是性别,而另一个可以是国家等我喜欢结构矿是这样的:

Finally, another option is a Lookup table. Basically, you keep key-value pairs associated with a Lookup type. One example of a type may be gender, while another may be State, etc. I like to structure mine like this:

ID | LookupType | LookupKey | LookupValue | LookupDescription | Active
1  | Gender     | 1         | Male        | male gender       | 1
2  | State      | 50        | Hawaii      | 50th state        | 1
3  | Gender     | 2         | Female      | female gender     | 1
4  | State      | 49        | Alaska      | 49th state        | 1
5  | OrderType  | 1         | Web         | online order      | 1

我喜欢用这些表时,一组数据不经常变化,但仍然需要枚举不时。

I like to use these tables when a set of data doesn't change very often, but still needs to be enumerated from time to time.

希望这有助于!

这篇关于使用实体框架(型号的.edmx)及MVC3创建一个下拉列表;剃刀观点与放大器;&安培;插入数据库记录到多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆