MVC Html.DropDownListFor using Model not ViewBag?

I've used ViewBag to load a Html.DropDownListFor because I learned it from the MVC Music Store project. I would like to change my coding style just a bit and begin using a Model. Here is my table model, just unsure how to build the special ViewModel for the Html.DropDownListFor.

[Current View]
<div class="editor-field">
            @Html.DropDownList("CategoryID", ViewBag.CategoryID as SelectList,"- select -", new { id = "catId", onchange = "subList()" })
        </div>

Open in new window

[Current ViewBag Style]
public ActionResult _New()
        {
            ViewBag.CategoryID = new SelectList(context.CategoryRefs, "CategoryID", "Name");

            return View();
        }

Open in new window

[Current Model]
namespace BudgetProjectUI
{
    using System;
    using System.Collections.Generic;
    
    public partial class CategoryRef
    {
        public CategoryRef()
        {
            this.CategoryAsgns = new HashSet<CategoryAsgn>();
            this.Budgets = new HashSet<Budget>();
        }
    
        public System.Guid CategoryID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<CategoryAsgn> CategoryAsgns { get; set; }
        public virtual ICollection<Budget> Budgets { get; set; }
    }
}

Open in new window

[Proposed View using Model and Html.DropDownListFor]
<div class="editor-field">
            @Html.DropDownListFor(m => m.???, new SelectList(Model.???, "Key", "Value", Model.???))
        </div>

Open in new window

[Proposed New Model]
Help, I need a Model, thx
WorknHardrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StephanLead Software EngineerCommented:
In order to apply a model to a view, you need to tell the view which model is active. This can be done by setting the below line in the view (at the top of the view);

@model BudgetProjectUI.CategoryRef

Open in new window


Now you should have intellisense on the Model part and for the DropDownListFor.

Let say we want to do the following:
- CategoryID is our value we would like to set
- CategoryAsgns is our collection of categories to select.
- If we set the categoryId, this needs to be the selected value

It would look something like this:

@Html.DropDownListFor(m => m.CategoryID, new SelectList(Model.???, "CategoryID", "Name", Model.CategoryID))

Open in new window

0
WorknHardrAuthor Commented:
This is the CategoryViewModel I need to use, sorry my mistake for posting CategoryRef above.

[View Model]
public class CategoryViewModel
    {
        public CategoryAsgn categoryAsgn { get; set; }
        public CategoryRef categoryRef { get; set; }
        public CategorySubRef categorySubRef { get; set; }
    }

 public partial class CategoryAsgn
    {
        public CategoryAsgn()
        {
            this.Accounts = new HashSet<Account>();
            this.Expenses = new HashSet<Expense>();
            this.Incomes = new HashSet<Income>();
        }
    
        public System.Guid CategoryAsgnID { get; set; }
        public System.Guid CategoryID { get; set; }
        public System.Guid CategorySubID { get; set; }
    
        public virtual ICollection<Account> Accounts { get; set; }
        public virtual CategoryRef CategoryRef { get; set; }
        public virtual CategorySubRef CategorySubRef { get; set; }
        public virtual ICollection<Expense> Expenses { get; set; }
        public virtual ICollection<Income> Incomes { get; set; }
    }

public partial class CategoryRef
    {
        public CategoryRef()
        {
            this.CategoryAsgns = new HashSet<CategoryAsgn>();
            this.Budgets = new HashSet<Budget>();
        }
    
        public System.Guid CategoryID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<CategoryAsgn> CategoryAsgns { get; set; }
        public virtual ICollection<Budget> Budgets { get; set; }
    }

 public partial class CategorySubRef
    {
        public CategorySubRef()
        {
            this.CategoryAsgns = new HashSet<CategoryAsgn>();
        }
    
        public System.Guid CategorySubID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<CategoryAsgn> CategoryAsgns { get; set; }
    }

Open in new window

0
StephanLead Software EngineerCommented:
For the dropdownlist you can use the viewmodel, but if you want to have a selectlist, it's best to use ViewData dictionary to get a list of categories.
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

WorknHardrAuthor Commented:
Are you saying that the only way to load a DropDownList with both key/value pairs is to use either ViewBag or ViewData?
0
StephanLead Software EngineerCommented:
No it's not the only way, it's better to do it because I think a list of categories should not be part of your model only the selected value.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WorknHardrAuthor Commented:
Ok, so I'm bak whee I started before posting this question. Well then do you think using ViewData is better than ViewBag?
0
StephanLead Software EngineerCommented:
ViewBag is just a wrapper for viewdata.

Here are some details:
http://www.rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications
0
WorknHardrAuthor Commented:
Thanks for your expert advice and helpful 'Model Binding' link...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.