15

Several views from my project have the same dropdownlist...

So, in the ViewModel from that view I have :

public IEnumerable<SelectListItem> FooDdl { get; set; }

And in the controller I have :

var MyVM = new MyVM() {
    FooDdl = fooRepository.GetAll().ToSelectList(x => x.Id, x => x.Name)
}

So far so good... But I´m doing the same code in every view/controller that have that ddl...

Is that the best way to do that?

Thanks

Paul
  • 12,359
  • 20
  • 64
  • 101
  • Are you talking about lots of different drop down lists or just the one drop down list repeating? Is caching a concern? – GraemeMiller Oct 08 '13 at 15:03
  • use a partial view with child action – Dave Alperovich Oct 08 '13 at 16:00
  • I have that "problem" with about 4 drowpdowns ... And I´m using Generics repository pattern with Ioc... – Paul Oct 08 '13 at 16:25
  • Do you foresee your code base growing and having this issue with more dropdowns? My answer is probably more applicable if you see this happening a lot. If you don't there are probably simpler ways to handle it. I'd probably just create a select list provider service that you can inject and call as required. We have hundreds of ViewModels some with 10 dropdowns so my solution would help if you are going to grow the application a lot. – GraemeMiller Oct 08 '13 at 17:44
  • What part of the code would you like to be more generic. The `.ToSelectList()`? – Henk Mollema Oct 08 '13 at 20:23

13 Answers13

3

I'd say that's fine to be honest, as it's only a repeat of a few lines of code. If it's really bothering you though, you could have all your controllers inherit from a BaseController (if they don't already) and store a method in there to get them all, something like:

public IEnumerable<SelectListItem> GetFoos()
{
    return fooRepository.GetAll().ToSelectList(x => x.Id, x => x.Name);
}

Then in your controllers you could do:

var MyVM = new MyVM() {
    FooDdl = GetFoos()
}
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • Yeah fine if he is just talking about one DDL and one repository. Passing repositories to base controller is never ideal especially if the list keeps getting added to as change usually ends up requiring changes to lots of controllers. – GraemeMiller Oct 08 '13 at 15:06
3

If your DropDownList is exactly the same the approach I would use is:

1) In your Base Controller or in a Helper class, you can create a method that returns a SelectList. That method should receive a nullabe int to get the select list with a value pre selected.

2) It is wise to cache the information you list in the DDL, to not query the database too often.

So, for (1):

public SelectList GetMyDDLData(int? selectedValue){
    var data = fooRepository.GetAll().Select(x => new { Value = x.Id, Text = x.Name });
    return new SelectList(data, "Id","Name", selectedValue);
}

In the view model:

var myVM = new MyVM();
myVM.DDLData = this.GetMyDDLData(null) // if it is in your BaseController.
myVM.DDLData = YourHelperClass.GetMyDDLData(null) // if it is in a helper static class

In your views:

@Html.DropDownListFor(x => x.FooProp, Model.DDLData, "Select one...")

For number (2):

private IEnumerable<YourModel> GetMyData()
{
    var dataItems = HttpContext.Cache["someKey"] as IEnumerable<YourModel>;
    if (dataItems == null)
    {
        // nothing in the cache => we perform some expensive query to fetch the result
        dataItems = fooRepository.GetAll().Select(x => new YourModel(){ Value = x.Id, Text = x.Name };

        // and we cache it so that the next time we don't need to perform the query
        HttpContext.Cache["someKey"] = dataItems ;
    }

    return dataItems;
}

The "someKey" could be something specific and static is this data is the same to all users, or you can do "someKey" + User.Id if the data is specific to one user.

If your repository is an abstractin layer (not directly EntityFramework) you can place this code there.

Romias
  • 13,783
  • 7
  • 56
  • 85
3

We also use a static class :

public static class SelectLists
{
        public static IList<SelectListItem> CompanyClasses(int? selected)
        {
            var session = DependencyResolver.Current.GetService<ISession>();

            var list = new List<SelectListItem>
                           {
                               new SelectListItem
                                   {
                                       Selected = !selected.HasValue,
                                       Text = String.Empty
                                   }
                           };

            list.AddRange(session.All<CompanyClass>()
                              .ToList()
                              .OrderBy(x => x.GetNameForCurrentCulture())
                              .Select(x => new SelectListItem
                                               {
                                                   Selected = x.Id == (selected.HasValue ? selected.Value : -1),
                                                   Text = x.GetNameForCurrentCulture(),
                                                   Value = x.Id.ToString()
                                               })
                              .ToList());

            return list;
        }
}

In the view we have nothing special :

@Html.DropDownListFor(x => x, SelectLists.CompanyClasses(Model))

And sometime we also create an EditorTemplate so it's faster to reuse like this

Model :

[Required, UIHint("CompanyClassPicker")]
public int? ClassId { get; set; }

EditorTemplate :

@model int?

@if (ViewBag.ReadOnly != null && ViewBag.ReadOnly)
{
    var item = SelectLists.CompanyClasses(Model).FirstOrDefault(x => x.Selected);

    if (item != null)
    {
        <span>@item.Text</span>
    }
}
else
{
    @Html.DropDownListFor(x => x, SelectLists.CompanyClasses(Model))    
}
VinnyG
  • 6,883
  • 7
  • 58
  • 76
2

Create object with getter for your dropdown values:

public static class DropDowns
{
    public static List<SelectListItem> Items { 
       get
       {
           //Return values
       } 
    } 
}

Create Razor partial:

@Html.DropDownListFor(m => "ChoosenItem", DropDowns.Items, "")

Call partial:

@Html.RenderPartial("DropDownItems")

And finally receive ChoosenItem value in controller. Simply.

1

I use an IModelEnricher combined with Automapper and attributes that define relationships between a type of list and select list provider. I return an entity etc using a specific ActionResult that then automaps my entity to a ViewModel and enriches with data required for select lists (and any additional data required). Also keeping the select list data as part of your ViewModel keeps your controller, model, and view responsibilities clear.

Defining a ViewModel ernicher means that anywhere that ViewModel is used it can use the same enricher to get its properties. So you can return the ViewModel in multiple places and it will just get populated with the correct data.

In my case this looks something like this in the controller:

public virtual ActionResult Edit(int id)
{
    return AutoMappedEnrichedView<PersonEditModel>(_personRepository.Find(id));
}

[HttpPost]
public virtual ActionResult Edit(PersonEditModel person)
{
     if (ModelState.IsValid){
            //This is simplified (probably don't use Automapper to go VM-->Entity)
            var insertPerson = Mapper.Map<PersonEditModel , Person>(person);
            _personRepository.InsertOrUpdate(insertPerson);
            _requirementRepository.Save();
            return RedirectToAction(Actions.Index());
      }
     return EnrichedView(person);
 }

This sort of ViewModel:

public class PersonEditModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public int FavouriteTeam { get; set; }
    public IEnumerable<SelectListItem> Teams {get;set;}
}

With this sort of Enricher:

public  class PersonEditModelEnricher :
IModelEnricher<PersonEditModel>
{
    private readonly ISelectListService _selectListService;

    public PersonEditModelEnricher(ISelectListService selectListService)
    {
        _selectListService = selectListService;
    }

    public PersonEditModelEnrich(PersonEditModel model)
    {
        model.Teams = new SelectList(_selectListService.AllTeams(), "Value", "Text")
        return model;
    }
} 

One other option is to decorate the ViewModel with attributes that define how the data is located to populate the select list. Like:

  public class PersonEditModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public int FavouriteTeam { get; set; }
        [LoadSelectListData("Teams")]
        public IEnumerable<SelectListItem> Teams {get;set;}
    }

Now you can decorate an appropriate method in your select service with an attribute like:

   [ProvideSelectData("Teams")]
   public IEnumerable Teams()
   {
        return _teamRepository.All.ToSelectList(a => a.Name, a => a.TeamId);
   }

Then for simple models with no complex enrichment just the generic enrichment process can handle it. If you want to do anything more complex you can define an enricher and it will be used if it exists.

Other options could be a convention over configuration approach where the Enricher looks at property name and type e.g. IEnumerable<SelectListItem> PossibleFirstDivisionTeams {get;set;} then matches this if it exists with a select list provider name in a class that say implements a marker interface ISelectListProvider. We went the attribute based one and just created Enums representing the various lists E.g. SelectList.AllFirstDivisionTeams. Could also try interfaces on ViewModel that just have a property collection for a selectlist. I don't really like interfaces on my ViewModels so we never did this

It all really depends on the scale of your application and how frequently same type of select list data is required across multiple models. Any specific questions or points you need clarified let me know

See this question. Also this blog post and this. Also this question on Automapper forum

Community
  • 1
  • 1
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
1

The first question is if the options-list belongs to the ViewModel. A year or two ago I did the same, but what I see recently more and more as a "best practice" is that people add the list to the ViewBag/ViewData not to the ViewModel. That's an option and I tend to do the same for a one-shot drop-down list, but it doesn't answer the code-reuse question you are facing. For that I see two different approaches (and two more that I rule out).

Shared editor template. Create an editor template for the type that's represented by the dropdown. In this case - because we don't have the list of possible options in the ViewModel or the ViewBag - the template has to reach out for the options to the server. That's possible by adding an action method (that returns json) to a controller class. Either to a shared "LookupsController" (possibly an ApiController) or to the controller that the list-items' type belongs to.

Partial view. The drop down values belong to some type. The Controller of that type could have an action method that returns a partial view.

The benefit of the first one is that a nice @Html.EditorFor call will do the job. But I don't like the ajax dependency. Partly for that reason I would prefer the partial view.

And there is a third one: child action, but I don't see that a good pattern here. You can google what's the difference between child actions and partial views, for this case child action is the wrong choice. I also wouldn't recommend helper methods. I believe they are not designed for this use case either.

peterfoldi
  • 7,451
  • 5
  • 21
  • 19
0

You could put that fetch in the default (null) constructor of MyVM if you don't need to vary its content.

Or you could use a PartialView that you render into the views that need t.

Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
  • This presumes you're using DI to configure your repository so your viewmodels can find it. If you aren't, you could still do this, injecting the repository in the ctor you use to instantiate the VM. – Tetsujin no Oni Oct 02 '13 at 20:08
0

If you really don't want to duplicate the code, place the code from the controllers into a helper class, and render the dropdown within a shared view (like _Layout.cshtml) that you'd then have to implement into your views by RenderPartial.

Create a partial view, _MyDropdownView.cstml, which uses the helper class you threw the code from the controllers in with something like the following:

@using MyNamespace.MyHelperClass
<div id="myDropdown">@Html.DropDownListFor(model => model.Prop, MyVM as SelectList, "--Select a Property--")</div>

Then, within your views:

@Html.RenderPartial("_MyDropdownView")
steamrolla
  • 2,373
  • 1
  • 29
  • 39
  • Render section will act similarly as well. Place in the views where it's needed, call helper class here: `@section myDropdown {
    @Html.DropDownListFor(model => model.Prop, MyVM as SelectList, "--Select a Property--")
    }` In a shared view: `@RenderSection("myDropdown", required: false)`
    – steamrolla Oct 04 '13 at 13:21
0

I like to use static classes often in a helper class that I can call from any view.

@Html.DropDownListFor(x => x.Field, PathToController.GetDropDown())

and then in your controller have a method built like this

public static List<SelectListItem> GetDropDown()
    {
        List<SelectListItem> ls = new List<SelectListItem>();
        lm = (call database);
        foreach (var temp in lm)
        {
            ls.Add(new SelectListItem() { Text = temp.name, Value = temp.id });
        }
        return ls;
    }

Hopefully it helps.

Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
0

Extension methods to the rescue

public interface ISelectFoo {    
    IEnumerable<SelectListItem> FooDdl { get; set; }
}

public class FooModel:ISelectFoo {  /* implementation */ }     

public static void PopulateFoo(this ISelectFoo data, FooRepository repo)
{
    data.FooDdl = repo.GetAll().ToSelectList(x => x.Id, x => x.Name);
}


//controller
var model=new ViewModel(); 
model.PopulateFoo(repo);


 //a wild idea
public static T CreateModel<T>(this FooRepository repo) where T:ISelectFoo,new()
{
    var model=new T();
    model.FooDdl=repo.GetAll().ToSelectList(x => x.Id, x => x.Name);
    return model;
 }

//controller
 var model=fooRepository.Create<MyFooModel>();
MikeSW
  • 16,140
  • 3
  • 39
  • 53
0

What about a Prepare method in a BaseController?

public class BaseController : Controller
{
    /// <summary>
    /// Prepares a new MyVM by filling the common properties.
    /// </summary>
    /// <returns>A MyVM.</returns>
    protected MyVM PrepareViewModel()
    {
        return new MyVM()
        {
            FooDll = GetFooSelectList();
        }
    }

    /// <summary>
    /// Prepares the specified MyVM by filling the common properties.
    /// </summary>
    /// <param name="myVm">The MyVM.</param>
    /// <returns>A MyVM.</returns>
    protected MyVM PrepareViewModel(MyVM myVm)
    {
        myVm.FooDll = GetFooSelectList();
        return myVm;
    }

    /// <summary>
    /// Fetches the foos from the database and creates a SelectList.
    /// </summary>
    /// <returns>A collection of SelectListItems.</returns>
    private IEnumerable<SelectListItem> GetFooSelectList()
    {
        return fooRepository.GetAll().ToSelectList(foo => foo.Id, foo => x.Name);
    }
}

You can use this methods in the controller:

public class HomeController : BaseController
{
    public ActionResult ActionX()
    {
        // Creates a new MyVM.
        MyVM myVm = PrepareViewModel();     
    }

    public ActionResult ActionY()
    {
        // Update an existing MyVM object.
        var myVm = new MyVM
                       {
                           Property1 = "Value 1",
                           Property2 = DateTime.Now
                       };
        PrepareViewModel(myVm);
    }
}
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
0

Have an interface with all your properties that need to be automatically populated:

public interface ISelectFields
{
    public IEnumerable<SelectListItem> FooDdl { get; set; }
}

Now all your view models that want to have those properties, implement that interface:

public class MyVM : ISelectFields
{
    public IEnumerable<SelectListItem> FooDdl { get; set; }
}

Have a BaseController, override OnResultExecuting, find the ViewModel that is passed in and inject the properties to the interface:

public class BaseController : Controller
{
    protected override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var viewResult = filterContext.Result as ViewResult;
        if (viewResult != null)
        {
            var viewModel = viewResult.Model as ISelectFields;
            if (viewModel != null)
            {
                viewModel.FooDdl = fooRepository.GetAll().ToSelectList(x => x.Id, x => x.Name)
            }
        }
        base.OnResultExecuting(filterContext);
    }
}

Now your controllers are very simple, everything is strongly typed, you are sticking with the DRY principle and you can just forget about populating that property, it will always be available in your views as long as your controllers inherit from the BaseController and your ViewModels implement the interface.

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        MyVM vm = new MyVM();
        return View(vm);   //you will have FooDdl available in your views
    }
}
Davor Zlotrg
  • 6,020
  • 2
  • 33
  • 51
0

Why not use the advantages of RenderAction: @(Html.RenderAction("ControllerForCommonElements", "CommonDdl"))

Create a controller, and an action that returns the Ddl and and just reference it in the views.

See some tips here on how you could use it

This way you can also cache this result. Actually the guys building StackOverflow talked about the pros of using this combined with different caching rules for different elements (i.e. if the ddl does not need to be 100% up to date you could cache it for a minute or so) in a podcast a while ago.

cfs
  • 1,304
  • 12
  • 30