使用asp.net中另一个表中的值生成下拉列表。vbmvc4

使用asp.net中另一个表中的值生成下拉列表。vbmvc4,asp.net,asp.net-mvc-4,dropdownlistfor,Asp.net,Asp.net Mvc 4,Dropdownlistfor,我已经创建了与数据库表相关的模型类和控制器,并希望填充另一个表的下拉列表。我的型号和控制器代码如下:- Imports System.Data.Entity Namespace employee1 Public Class EmployeeController Inherits System.Web.Mvc.Controller Private db As New EmployeeDBContext ' ' GET: /Emplo

我已经创建了与数据库表相关的模型类和控制器,并希望填充另一个表的下拉列表。我的型号和控制器代码如下:-

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
我想在create视图中从另一个名为IDStatus的表中为status创建一个下拉列表

@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
@使用Html.BeginForm()
@Html.ValidationSummary(True)
@
雇员模型
@LabelFor(函数(模型)model.CompanyCode)
@EditorFor(函数(模型)model.CompanyCode)
@Html.ValidationMessageFor
终端使用

您可以添加一个或多个属性作为同一模型的集合,也可以将该集合填充到ViewBage中,并在视图中使用它

At控制器

ViewBag.DropDownCollection = "Collection"
观看时

@Html.DropDownList("dropDownName",ViewBag.DropDownCollection)

您可以添加一个或多个属性作为同一模型的集合,也可以在ViewBage中填充该集合并在视图中使用它

At控制器

ViewBag.DropDownCollection = "Collection"
观看时

@Html.DropDownList("dropDownName",ViewBag.DropDownCollection)

您可以添加一个或多个属性作为同一模型的集合,也可以在ViewBage中填充该集合并在视图中使用它

At控制器

ViewBag.DropDownCollection = "Collection"
观看时

@Html.DropDownList("dropDownName",ViewBag.DropDownCollection)

您可以添加一个或多个属性作为同一模型的集合,也可以在ViewBage中填充该集合并在视图中使用它

At控制器

ViewBag.DropDownCollection = "Collection"
观看时

@Html.DropDownList("dropDownName",ViewBag.DropDownCollection)

不要忘记将ViewBag对象强制转换为SelectList,不要使用
@Html.DropDownList()
。始终使用强类型助手
@Html.DropDownListFor(m=>m.YourProperty,(SelectList)ViewBag.YourCollection)
嘿,Moksh Shah,我尝试了你的代码,但显示错误。看一看screencast@Prathamhatre用dotnet fiddle示例更新了我的答案,看一看,因为我没有使用VB,所以我对您的代码不太了解。不要忘记将ViewBag对象强制转换为SelectList,不要使用
@Html.DropDownList()
。始终使用强类型助手
@Html.DropDownListFor(m=>m.YourProperty,(SelectList)ViewBag.YourCollection)
嘿,Moksh Shah,我尝试了你的代码,但显示错误。看一看screencast@Prathamhatre用dotnet fiddle示例更新了我的答案,看一看,因为我没有使用VB,所以我对您的代码不太了解。不要忘记将ViewBag对象强制转换为SelectList,不要使用
@Html.DropDownList()
。始终使用强类型助手
@Html.DropDownListFor(m=>m.YourProperty,(SelectList)ViewBag.YourCollection)
嘿,Moksh Shah,我尝试了你的代码,但显示错误。看一看screencast@Prathamhatre用dotnet fiddle示例更新了我的答案,看一看,因为我没有使用VB,所以我对您的代码不太了解。不要忘记将ViewBag对象强制转换为SelectList,不要使用
@Html.DropDownList()
。始终使用强类型助手
@Html.DropDownListFor(m=>m.YourProperty,(SelectList)ViewBag.YourCollection)
嘿,Moksh Shah,我尝试了你的代码,但显示错误。看一看屏幕广播@Prathamhatre用dotnet fiddle示例更新了我的答案,看一看,因为我没有使用VB,所以我对你的代码不太了解