0

I need to create a Helper to build Dropdownlists used in several Controller Methods.

namespace Heelp.Helpers
{
    public class PTDistrictHelpers
    {
        public IEnumerable<SelectListItem> DropDownList(IList<PTDistrictDto> ptDistrictsDto, int selectedValue = GlobalConstants.DROPDOWNLIST_NO_SELECTED_VALUE)
        {
            var ptDistrictsViewModel = Mapper.Map<IList<PTDistrictDto>, IList<PTDistrictViewModel>>(ptDistrictsDto);

            var ptDistrictsList = ptDistrictsViewModel.Select(district =>
                new SelectListItem
                {
                    Value = district.Id.ToString(),
                    Text = district.Name,
                    Selected = (district.Id == selectedValue)
                });

            return ptDistrictsList;
        }
    }
}

The Controller method:

    [AllowAnonymous]
    public virtual PartialViewResult AdvancedSearch()
    {
       // District
        var dropdown = new Heelp.Helpers.PTDistrictHelpers();

        dropdown. <== The DropDownList method don't appears when I hit the "." why?
    }

The idea is to have a class in the Web Project where I can put all the code I will user in several places in the project that calls Service Layer Method not available in the Business Layer because they are related with the Web Layer like SelectListItem

1 Answer 1

1

If you plan to return a PartialViewResult then you might as well return a model that will be used in a partial view to build up the drop-down list.

[AllowAnonymous]
public virtual PartialViewResult AdvancedSearch()
{
   // The dropdown should return some enumerable list, like IEnumerable<MyModel>
    var dropdown = new Heelp.Helpers.PTDistrictHelpers();

    return PartialView("_MyPartialViewWithDropDownList", dropdown);
}

Your drop-down will be in a partial view that you can place in a Shared folder if you plan to reuse it in a number of different views.

@model IEnumerable<MyModel>

@Html.DropDownList("SomeName", new SelectList(Model, "ValueProperty", "TextProperty"), "Please choose an option", null)

This should be enough for you to reuse the drop down list. That way, you will not do a view's job in a controller. Controller should return the model, and view should do the rendering.

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

5 Comments

Hi thanks, but you don't call the method DropDownList from the class PTDistrictHelpers ? If you want in the future to had more methods in this class, how can you had and call them?
The point is that you should not work with HTML in a controller. On the other hand if you plan to expand yout control, then you should create a new helper method that extends HtmlHelper class. That way you can extend the helper with additional options.
My AdvancedSearch controller's method is responsable to build the model for the View, not only the dropdownlist, but all the data needed to be displayed beside the dropdown, so I'm having dificulties in uing and understanding your code in my project.
My code is intended to explain you how to avoid building HTML inside of a controller. If any part of your model (specifically a collection property), which is built by AdvancedSearch, can be used in order to populate the sample I wrote, you can use that collection to pass it over to the partial view in order to build up the drop down list.
Thanks, I have set your question has correct regarding the best practice it presents, and not has a direct answer for my question that I solved in a diferent way, but thanks anyway, I will try to use your code in the future for sure.

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.