ParserDoer
ParserDoer

Reputation: 85

ASP.NET MVC Populating DropDownListFor based on DB call

I am building a simple ASP.NET MVC 3 site, using Entity Framework to build the model based on some tables in an already existing Oracle database (in other words, I used the 'database first' methodology to have my model built). I now have a simple blog type site, which I am quite familiar with as I have been learning MVC in a number of languages.

I want to change some of the auto-generated views. One piece I would like to change in particular is that I have a field in one of my tables called 'Visible'. This is simply a numeric value (0 or 1) indicating whether or not a display application should use the row as display data. Currently, I have the simple text field that is auto-generated:

<div class="editor-field">
    @Html.EditorFor(model => model.VISIBLE)
    @Html.ValidationMessageFor(model => model.VISIBLE)
</div>

What I would like to do is replace this with a drop down box with string values like True and False. The application should display any entry with a 0 as false and vice versa. If a user wants to flip the toggle, the drop down should allow them to do that and understand to make the numeric update when clicking submit. How can this be done?

I have seen countless examples where the drop-down was going to be filled with more then just two values, and in those cases I understand that you can add logic to your controller that pulls all the distinct values, puts them in a list, then adds the list to the ViewBag. However, in my case with only two possible numeric values, it seems like there should be a simpler, more accepted way to do it.

Any help is greatly appreciated.

UPDATE

Following Quinton's answer, I am trying to place said code in my model. Here is my current model:

    namespace CurrentActivityBlog
    {
        using System;
        using System.Collections.Generic;
        using System.Web.UI.WebControls;

        public partial class TBLCURRENTACTIVITY
        {
            public string TITLE { get; set; }
            public string DESCRIPTION { get; set; }
            public System.DateTime DATETIME { get; set; }
            public short VISIBLE { get; set; }
            public decimal ID { get; set; }

            public IEnumerable<SelectedListItem> PossibleValues { get; set; }

        public TBLCURRENTACTIVITY() {
          PossibleValues = new[] { new SelectListItem { Value = "0", Text = "Hidden"       },              
              new SelectListItem { Value = "1", Text = "Visible" } }; 
        }

        }
    }

I am unable to build this solution, but Visual Studio 2010 is telling me that

"the type or namespace name 'SelectedListItem' could not be found (are you missing a using directive or an assembly reference?)"

As you can see, I have

using System.Web.UI.controls

and have added the reference to System.Web. Is there anything I am forgetting, or anything I should know about (such as models generated using EF behaving differently then one might expect, etc.)?

Thanks again.

Upvotes: 0

Views: 532

Answers (1)

Quinton Bernhardt
Quinton Bernhardt

Reputation: 4803

The one line solution to it would be:

@Html.DropDownList(Model.VISIBLE.ToString(), new [] {new SelectListItem { Value = "0", Text = "Hidden"}, new SelectListItem { Value = "1", Text = "Visible"}}) 

but you probably don't want domain logic in your view. So add the possible items to your Model (or from the controller):

    public class MyModel {
        public int VISIBLE { get; set; }
        public IEnumerable<SelectListItem> PossibleValues { get; set; }

        public MyModel() {
            PossibleValues = new[] { new SelectListItem { Value = "0", Text = "Hidden" }, new SelectListItem { Value = "1", Text = "Visible" } }; 
        }
    }

and then your razor code:

    @Html.DropDownList(Model.VISIBLE.ToString(), Model.PossibleValues)

Obviously "Hidden" and "Visible" descriptions can be replaced with "False" and "True" or whatever.

You could also create and Editor and Display Template for that specific field. Checkout ScottGu's blog post here, search for "UI Helper Templating Support" and you'll see how to create a editor template and how to render a specific template by name.

EDIT:

If your model is not part of your MVC project, then referencing any classes that are in the MVC assemblies would require a explicit add reference. You can avoid this though by initializing any MVC assembly types in your model from your controller, like such:

    public class MyModel {
            public int VISIBLE { get; set; }
            public IEnumerable<SelectListItem> PossibleValues { get; set; }
    }

controller action method:

    public ActionResult Edit(int Id) {
        ...
        myModelInstance. PossibleValues = new[] { new SelectListItem { Value = "0", Text = "Hidden" }, new SelectListItem { Value = "1", Text = "Visible" } }; 
        ...
        Return View(myModel);
    }

Upvotes: 1

Related Questions