I'm new to MVC4 programming. I want to populate a DropDownList from a table's field but don't know how to, although I searched for relevant material from Internet but couldn't get some workable info.

There are 2 tables Software and Category (showing the type of S/w e.g. Anti-Virus or Graphics or OS etc...). In Software table, CategoryID is in smallint form which has a foreign key relationship with the CategoryID in Category table (in which CategoryID and CategoryName are the only fields).

Now while creating the Create view to add/upload a new software, I want that the CategoryName of all the CategoryIDs be shown in a DropDown List, from which the user should choose the software's category. What I did is that I defined a method in my SoftwareRepository class (well, I took initiative from NerdDinner example so following that naming convention...) as follows:

private SoftwarePortalDataContext db = new SoftwarePortalDataContext();
public IQueryable<Category> FindAllCategories()
    {
        return db.Categories;
    }

And then defined the Create() of SoftwareControllers.cs as follows:

SoftwareRepository softwareRepository = new SoftwareRepository();
public ActionResult Create()
    {
        ViewBag.Categories = new SelectList(softwareRepository.FindAllCategories(),
                                             "CategoryID", "CategoryName");
        return View(new Software());
    }

But still the stmt @Html.DropDownListFor() inside Create.cshtml isn't working. What could possibly be the arguments for this? I tried various versions of this method but the signatures of C# methods are much more complex than C++ or Java's methods. So it doesn't seem to take some acceptable shape... :( This statement doesn't even compile....

-The model being used in my Create.cshtml file is @model SoftwarePortal.Models.Software

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.