3

I have a table named Scope in my databse, This table has two columns: ScopeName ad ScopeID. How can I pass all the data of ScopeName column to my controller and put them in an array? after that I want to show the content in a dropdown list in my view. Thanks

2
  • How are you currently getting data out of your db? What have you tried? Commented Jun 8, 2012 at 18:23
  • I'm using Database First approach. Commented Jun 8, 2012 at 18:56

2 Answers 2

3

If you can get ahold of an IQueryable<Scope> from your database, this LINQ code should work:

var scopeNames = (from s in DB.GetScopes()
                 select s.ScopeName).ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, Now how can I show this array in a dropdown list in my view?
2

following @Charmander answer..

To pass the data to the view you can do this:

In your controller create a SelectList with your array and pass it to your view:

ViewBag.myList = new SelectList(scopeNames);

Then in your view, use Html.DropDownlist:

@Html.DropDownList("myList", String.Empty)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.