I’m new in MVC3 and trying learn something. using Razor view engine. mysql database. I have a table where I keep the companies(companyID, companyName, etc.). I have a model with same attributes :
public class CompanyModel{
public int companyID;
public string companyName;
......
....
}
I want to create a dropdownlist which holds the companies, shows me the company name and when I select an item I need to be able to access the companyID. I can query the table and have all rows in a List of companies :
List<Company> companies = new List<Company>();
companies = getCompanies();
but I don’t know how to bind this list with the dropdownlist and how to get the selected value after call of a post method from my controller.
Any help would be appreciated.
You could use a view model:
and then have your controller action instantiate, populate and pass this view model to the view:
and finally a strongly typed view in which you could render an HTML form containing the dropdown list: