我已经创建了与数据库表相关的模型类和控制器,并希望从另一个表中填充一个下拉列表。我的模型和控制器代码如下:
Imports System.Data.Entity
Namespace employee1
Public Class EmployeeController
Inherits System.Web.Mvc.Controller
Private db As New EmployeeDBContext
'
' GET: /Employee/
Function Index(ByVal sortOrder As String) As ActionResult
ViewBag.LastNameSortParm = If(String.IsNullOrEmpty(sortOrder), "LastName_desc", String.Empty)
Dim Employee = From e In db.Employee Select e
Select Case sortOrder
Case "LastName_desc"
Employee = Employee.OrderByDescending(Function(e) e.LastName)
Case Else
Employee = Employee.OrderBy(Function(e) e.LastName)
End Select
Return View(Employee.ToList())
End Function
'
' GET: /Employee/Details/5
Function Details(Optional ByVal id As Integer = Nothing) As ActionResult
Dim employeemodel As EmployeeModel = db.Employee.Find(id)
If IsNothing(employeemodel) Then
Return HttpNotFound()
End If
Return View(employeemodel)
End Function
'
' GET: /Employee/Create
Function Create() As ActionResult
Return View()
End Function
这是我的模特
Imports System.Data.Entity
Public Class EmployeeModel
Public Property ID() As Integer
Public Property CompanyCode() As String
Public Property FirstName() As String
Public Property LastName() As String
Public Property DeptNum() As String
Public Property Status() As Char
Public Property txtCity() As String
Public Property txtState() As String
Public Property txtZip() As String
Public Property txtPhone() As String
Public Property txtPhoneExt() As String
Public Property LastReviewDate() As Date
Public Property HireDate() As Date
End Class
Public Class EmployeeDBContext
Inherits DbContext
Public Property Employee() As DbSet(Of EmployeeModel)
End Class
我想要从另一个名为IDStatus的表中获取状态的创建视图中的下拉列表,例如
@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
<legend>EmployeeModel</legend>
<div class="editor-label">
@Html.LabelFor(Function(model) model.CompanyCode)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.CompanyCode)
@Html.ValidationMessageFor
</div>
<!-- how do i use dropdown list here which is from different table?-->
</fieldset>
End Using
create viewmodel and pass both table properties in that
public class viewmodel
{
public table1 Table1{get;set;} //return your first table properties here
public table2 Table2{get;set;} //return your second table properties here
}
In view
@htnl.dropdownlistfor(m=>m.Table2.propertyname,-----)`enter code here`
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句