Skip to content

Instantly share code, notes, and snippets.

@rohitkandhal
Last active December 19, 2015 10:18
Show Gist options
  • Save rohitkandhal/5938952 to your computer and use it in GitHub Desktop.
Save rohitkandhal/5938952 to your computer and use it in GitHub Desktop.
Using drop-down list with ViewModel in ASP.net MVC4
Objective: In this page, we will see how to populate a drop-down list on View in ASP.net MVC4.
Setup: Suppose you have a Project entity with two properties:
a) Project Name
b) Project Status (can be Active, Inactive, Inprogress etc.)
Models > Project.cs
public partial class Project
{
// Scalar Properties
public int ProjectId {get;set;}
public string ProjectName {get;set;}
// Navigation Properties
public virtual StatusValue Status {get;set;}
}
Models > StatusValue.cs
// This class holds status values such as 1 - Active, 2 - Inactive.
public partial class StatusValue
{
public int Id {get;set;}
public string Name {get;set;}
}
In order to have decoupled Views and Models, we will create view modal with similar structure.
public class ProjectViewModel
{
public int ProjectId {get;set;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment