generate dropdown using values from another table in asp.net. vb mvc 4

1.7k views Asked by At

I have created model class and controller related to a database table and want to populate a dropdown list from another table. my model and controller code is below:-

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

and this is my model

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

I want a dropdown list in create view for status from another table named IDStatus view is like

@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
2

There are 2 answers

0
Mahesh On BEST ANSWER
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`
4
Mox Shah On

Either you can add one more property as a collection of the same model, or you can fill that collection in a ViewBage and use it the same at your view.

At Controller

ViewBag.DropDownCollection = "Collection"

At View

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

Fiddle