0

In my ASP.NET MVC application, I want to have dropdowns available to the user when creating and editing certain items. I have a table in my database dd_data, it looks like this: dd_name, dd_value, dd_type.

Thew views I want to display them on are completely unrelated. These are generic dropdowns for holding items such as language, currency, and days of week for example: "US Dollar", "840", "currency"

Please mind, my example are very generic, there is actually custom data in the table.

Should I create a model/controller/views for each dropdown? What is the best way to do this?

I have seen many examples of using DropDownForList, but the data is usually available in the model. In this case I want to use an unrelated table to populate a value in my current model with a preset.

4
  • Put the list into your ViewModel as a property. Reference that property in your DropDownForList. Or, you can put the list into the ViewBag and reference it from there. Commented Apr 11, 2013 at 20:16
  • I guess I am having trouble figuring out how to access the data, Do I need to create a context to access it? Commented Apr 11, 2013 at 20:23
  • There is some good general guidance here: odetocode.com/Blogs/scott/archive/2010/01/18/…. Although it is a bit outdated, it should give you the general idea. Commented Apr 11, 2013 at 20:25
  • Also here: stackoverflow.com/questions/7075496/… Commented Apr 11, 2013 at 20:26

1 Answer 1

1

Based on your question and the additional comment you left it sounds like you're relatively new to MVC, as well as unfamiliar with Entity Framework? In that case I highly recommend the free Pluralsight videos available on http://www.asp.net/mvc .

Check the links on the right sidebar, sounds like "Working with data part 1" is what you're after. They're great primers and will get you up and running quickly. Looks like the video's have been updated for VS2012 and MVC4, but if you're still on VS2010 and MVC3 they still have the older versions available here. Just click on the "Training from Pluralsight" link.

That said, if you have NuGet installed with VS you can add a quick reference to Entity Framework and have a code first context running real quick. If you add a connection string to your web.config and name it say ... "MyContext", then you could just add something like this to your project:

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace MyProject.CoolNameSpace
{
    public class MyContext : DbContext
    {
        public DbSet<dd_data> DataEntities { get; set; }
    }

    public class dd_data
    {
        public string dd_name { get; set; }
        public string dd_value { get; set; }
    }
}

And you'll now have a working context. A lousy one, these classes should be in their files with proper namespaces and such ... but you get the idea.

From there you can work on attaching your model to your View, and to answer your original question I'd use a viewmodel that exposed one of your dd_data lists as a property, a single dd_value for a selected property, then use a DropdownFor to create your select list.

ViewModel:

public class MyViewModel
{
    public List<dd_data> MyDataList { get; set; }
    public string SelectedValue { get; set; }
}

Somewhere in the View:

@Html.DropDownListFor(m => m.SelectedValue, new SelectList(Model.MyDataList, "dd_name", "dd_value"))

Hopefully that'll get you started, but seriously watch those PluralSight video's, they're great. Cheers.

Sign up to request clarification or add additional context in comments.

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.