1myb
1myb

Reputation: 3596

Get value from Entity Framework for Dropdown List and Return selected value in MVC

Assuming i got a table called Countries and using Entity Framework, i want to know how could i populate the available countries (listed in the table countries) to view as drop down list and return the value to HTTPPost Controller

i got

public ActionResult SignUp() 
i think the populate code should be here but i not sure 
how to retrieve from entity framework and populate into view

and

 [httpPost]
 public ActionResult SignUp()
 i want to read the user selected value and i think is 
 int value = form["DropDownListName"].SelectedIndex + 1;

can anyone please guide me on this with some hint or example , please ? Thx a lot =D

Upvotes: 0

Views: 950

Answers (1)

Steve Morgan
Steve Morgan

Reputation: 13091

To be honest, you're not really working in an MVC pattern here. Don't put UI construction logic in your constructor.

Rather, expose the ID that you want to bind to the list via a model that you pass to a View() method in your constructor.

In your view, use the name of that property as the Name of a Drop-down and create a helper class to generate the list of values.

I'd give you a more specific example, but I'm in the cinema with my iPad, so a bit stuck for access to Visual Studio at the moment!

Upvotes: 2

Related Questions