MVC 5 - 在单一视图中绑定模型和不同类型的模型数组



我正在使用Visual Studio 2013,MVC 5,EF 6,Visual Basic,Database First Approach构建一个项目。

在我的数据库中,我有一个表格Employee如下所示:

CREATE TABLE [dbo].[Employee]
(
    [Id] INT PRIMARY KEY IDENTITY (1, 1), 
    [FirstName] VARCHAR(50) NOT NULL, 
    [MiddleName] VARCHAR(50) NOT NULL, 
    [LastName] VARCHAR(50) NOT NULL, 
)

另一个表格Qualification如下:

CREATE TABLE [dbo].[Qualification]
(
    [Id] INT PRIMARY KEY IDENTITY (1, 1), 
    [EmployeeId] INT NOT NULL REFERENCES Employee,
    [Degree] VARCHAR(50) NOT NULL, 
    [Institute] VARCHAR(50) NULL,
)

我的EmployeeQualification模型如下所示:

Partial Public Class Employee
    Public Property Id As Integer
    Public Property FirstName As String
    Public Property MiddleName As String
    Public Property LastName As String
    Public Overridable Property Qualifications As ICollection(Of Qualification) = New HashSet(Of Qualification)
End Class
Partial Public Class Qualification
    Public Property Id As Integer
    Public Property EmployeeId As Integer
    Public Property Degree As String
    Public Property Institute As String
    Public Overridable Property Employee As Employee
End Class

现在,在我看来;我想让用户一次插入 1 名员工以及他的 5 个资格: 所以,我的观点是这样的:

@ModelType MyApp.Employee
@Using (Html.BeginForm()) 
    @Html.AntiForgeryToken()
    @<div class="form-horizontal">
        <h4>Employee</h4>
        <hr />
        @Html.ValidationSummary(True, "", New With { .class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(Function(model) model.FirstName, htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.FirstName, New With { .htmlAttributes = New With { .class = "form-control" } })
                @Html.ValidationMessageFor(Function(model) model.FirstName, "", New With { .class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(Function(model) model.MiddleName, htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.MiddleName, New With { .htmlAttributes = New With { .class = "form-control" } })
                @Html.ValidationMessageFor(Function(model) model.MiddleName, "", New With { .class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(Function(model) model.LastName, htmlAttributes:= New With { .class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.LastName, New With { .htmlAttributes = New With { .class = "form-control" } })
                @Html.ValidationMessageFor(Function(model) model.LastName, "", New With { .class = "text-danger" })
            </div>
        </div>
    @For i = 0 To 5
        @<div class="form-group">
            @Html.LabelFor(Function(model) model.Qualifications(i).Degree, htmlAttributes:=New With {.class = "control-label col-md-2"})
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.Qualifications(i).Degree, New With {.htmlAttributes = New With {.class = "form-control"}})
                @Html.ValidationMessageFor(Function(model) model.Qualifications(i).Degree, "", New With {.class = "text-danger"})
            </div>
        </div>
        @<div class="form-group">
            @Html.LabelFor(Function(model) model.Qualifications(i).Institute, htmlAttributes:=New With {.class = "control-label col-md-2"})
            <div class="col-md-10">
                @Html.EditorFor(Function(model) model.Qualifications(i).Institute, New With {.htmlAttributes = New With {.class = "form-control"}})
                @Html.ValidationMessageFor(Function(model) model.Qualifications(i).Institute, "", New With {.class = "text-danger"})
            </div>
        </div>
    Next

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
End Using

这是我在控制器中编写操作方法的方式:

Function Create(<Bind(Include:="Id,FirstName,MiddleName,LastName,Qualifications")> ByVal employee As Employee) As ActionResult
    ' Code here
End Function

现在,当我填写并提交表格时,我会得到我提交的值Employee例如。 FirstNameMiddleNameLastName;但Qualifications计数为 0。

我也试图使用Tuple解决问题,但没有奏效。

我能做什么?任何帮助将不胜感激。

自己得到了解决方案:

我将类EmployeeQualifications属性更改为:

Public Overridable Property Qualifications As List(Of Qualification) = New List(Of Qualification)

它就像一个魅力!

相关内容

最新更新