0

I'm trying to move over to MVC from webforms, and to be honest, it's not going very well.

I need a DropDownList populated with values from my database, and I'm having a hard time understanding how to do this. I'm thinking, that I need to create a model,using entity framework, and refer my DropDownList to that? But how do I point it to that model? Also, if I make a model, from a database table, how do I adjust the select command, so I only get certain values, and not the entire table?

I know this is kind of a broad question, and I have no examples listed, but I've had a really hard time, finding information I could understand, with regards to my issue.

4
  • at what point are you stuck are you creating a list as part of the model or just a list Commented Apr 30, 2012 at 7:17
  • I'm pretty much stuck, before I even start. I have no idea how to begin. I'm assuming the correct way, is to make a model, and somehow use that. Commented Apr 30, 2012 at 7:19
  • are you using code first or you already have db Commented Apr 30, 2012 at 7:21
  • I have the DB already. I have a table with LOV values, but I need to use only the ones, with a special ListName. Commented Apr 30, 2012 at 7:23

3 Answers 3

1

I would start from this this should get you the project created if you have not done so so you can have the model ready

http://msdn.microsoft.com/en-us/data/gg685489

in order to create a dropdownlist here is an example

ViewBag.dropdownlist = new SelectList(db.tablename, "Valuefiled", "NameGField");

where Valuefiled=name of a column in your database that you want to use for values

"NameGField"=name of a column in your database that you want to use for names

getting drop down list to view

@Html.DropDownList("dropdownlist")
Sign up to request clarification or add additional context in comments.

2 Comments

How do I filter this then? I want to only select the entries, where the column ListNAme is a certain value.
Fix it by adding a ToList().Where() to the db.tablename in the selectlist constructor.
1

How About this

Your ViewModel

   public class CategoryViewModel
   {
    public Category Category { get; set; }   
    public IEnumerable<SelectListItem> CategoryTitles { get; set; }   
   }

Your Controller

 public ActionResult Create()
    {
       var categoryviewmodel = new CategoryViewModel();              
       categoryviewmodel.Category = new Category();
       var list = categoryRepository.AllCategoryTitles().ToList().Select(t => new SelectListItem
          {
                Text = t.CategoryName,
                Value = t.CategoryID.ToString()
          })
          .ToList();           
          list.Insert(0, new SelectListItem { Value = "0", Text = "Please Selext" });          

        categoryviewmodel.CategoryTitles = list;
        return View(categoryviewmodel);
    } 

Your Repository

    public IQueryable<Category> AllCategoryTitles()
    {       
        var query = context.Categories.Where(m => m.ParentCategoryID == null && m.IsActive==true);
        return query;
    }

Your View

  @Html.DropDownListFor(model => model.CategoryParentID, Model.CategoryTitles)

Comments

0

You can use a viewModel. Here is an example solution with some assumptions. Refer to the dropdownlist (here in the dropdown I am listing departments of type "InBound"

Employee Model

public class EmployeeModel
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int DeptId { get; set; }
}

Department Model

public class DepartmentModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
}

ViewModel (to be passed into the view)

public class EmployeeViewModel
{
    public EmployeeModel Employee { get; set; }
    public IEnumerable<DepartmentModel> Departments { get; set; }
}

Controller

public ActionResult Index()
    {
        EmployeeViewModel vm = new EmployeeViewModel();

        //This is hardcoded. However you will write your own method to pull the department details with filtering
        List<DepartmentModel> departments = new List<DepartmentModel>() { new DepartmentModel { Id = 1, Name = "Accounts", Type = "InBound" }, new DepartmentModel { Id = 2, Name = "Finance", Type = "OutBound" }, new DepartmentModel { Id = 3, Name = "HR", Type = "InBound" } };
        vm.Departments = departments.Where(d => d.Type == "InBound"); 

        return View(vm);
    }

View

    @model Test1.ViewModels.EmployeeViewModel
    @{
          ViewBag.Title = "Index";
     }

     <h2>Index</h2>
     @using (Html.BeginForm())
     {
         @Html.HiddenFor(model => model.Employee.Id);
      <table>
        <tr>
            <td>@Html.LabelFor(model => model.Employee.FirstName)</td>
            <td>@Html.EditorFor(model => model.Employee.FirstName)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(model => model.Employee.LastName)</td>
            <td>@Html.EditorFor(model => model.Employee.LastName)</td>
        </tr>
        <tr>
            <td>@Html.Label("Department")</td>
            <td>@Html.DropDownListFor(model => model.Employee.DeptId, new SelectList(Model.Departments, "Id", "Name"))</td>
        </tr>
    </table>
}

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.