我正在使用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,
)
我的Employee
和Qualification
模型如下所示:
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
提交的值,例如。 FirstName
,MiddleName
和LastName
;但Qualifications
计数为0。
我也尝试使用Tuple
来解决问题,但是没有用。
我该怎么办?任何帮助,将不胜感激。
答案 0 :(得分:0)
自己找到解决方案:
我将Qualifications
类的Employee
属性更改为:
Public Overridable Property Qualifications As List(Of Qualification) = New List(Of Qualification)
它就像一个魅力!